def do_component(parser, token): """ {% component_block "name" variable="value" variable2="value2" ... %} """ bits = token.split_contents() tag_args, tag_kwargs = parse_bits( parser=parser, bits=bits, params=["tag_name", "component_name"], takes_context=False, name="component_block", **PARSE_BITS_DEFAULTS ) tag_name = tag_args.pop(0) if len(bits) < 2: raise TemplateSyntaxError( "Call the '%s' tag with a component name as the first parameter" % tag_name ) component_name = bits[1] if not component_name.startswith(('"', "'")) or not component_name.endswith( ('"', "'") ): raise TemplateSyntaxError( "Component name '%s' should be in quotes" % component_name ) component_name = component_name.strip('"\'') component_class = registry.get(component_name) component = component_class() extra_context = {} if len(bits) > 2: extra_context = component.context(**token_kwargs(bits[2:], parser)) slots_filled = NodeList() tag_name = bits[0] while tag_name != "endcomponent_block": token = parser.next_token() if token.token_type != TokenType.BLOCK: continue tag_name = token.split_contents()[0] if tag_name == "slot": slots_filled += do_slot(parser, token, component=component) elif tag_name == "endcomponent_block": break return ComponentNode(component, extra_context, slots_filled)
def process_response_content(content): from django_components.component import registry component_names_seen = { match.group("name") for match in COMPONENT_COMMENT_REGEX.finditer(content) } all_components = [ registry.get(name.decode("utf-8"))("") for name in component_names_seen ] all_media = join_media(all_components) js_dependencies = b"".join( media.encode("utf-8") for media in all_media.render_js()) css_dependencies = b"".join( media.encode("utf-8") for media in all_media.render_css()) return PLACEHOLDER_REGEX.sub( DependencyReplacer(css_dependencies, js_dependencies), content)
def parse_component_with_args(parser, bits, tag_name): tag_args, tag_kwargs = parse_bits( parser=parser, bits=bits, params=["tag_name", "name"], takes_context=False, name=tag_name, varargs=True, varkw=[], defaults=None, kwonly=[], kwonly_defaults=None, ) assert ( tag_name == tag_args[0].token ), "Internal error: Expected tag_name to be {}, but it was {}".format( tag_name, tag_args[0].token) if ( len(tag_args) > 1 ): # At least one position arg, so take the first as the component name component_name = tag_args[1].token context_args = tag_args[2:] context_kwargs = tag_kwargs else: # No positional args, so look for component name as keyword arg try: component_name = tag_kwargs.pop("name").token context_args = [] context_kwargs = tag_kwargs except IndexError: raise TemplateSyntaxError( "Call the '%s' tag with a component name as the first parameter" % tag_name) if not is_wrapped_in_quotes(component_name): raise TemplateSyntaxError("Component name '%s' should be in quotes" % component_name) trimmed_component_name = component_name[1:-1] component_class = registry.get(trimmed_component_name) component = component_class(trimmed_component_name) return component, context_args, context_kwargs
def component_tag(name, *args, **kwargs): component_class = registry.get(name) component = component_class() return component.render(*args, **kwargs)