1010from django .template .context import Context
1111from django .template .loader import select_template
1212from django .utils .safestring import SafeString
13- from django .utils .safestring import mark_safe
1413
1514from django_bird ._typing import TagBits
1615from django_bird ._typing import override
1716from django_bird .components .attrs import Attrs
17+ from django_bird .components .slots import DEFAULT_SLOT
18+ from django_bird .components .slots import Slots
1819from django_bird .components .templates import get_template_names
1920
20- from .slot import SlotNode
21-
2221TAG = "bird"
2322END_TAG = "endbird"
2423
@@ -62,7 +61,6 @@ def __init__(self, name: str, attrs: list[str], nodelist: NodeList | None) -> No
6261 self .name = name
6362 self .attrs = attrs
6463 self .nodelist = nodelist
65- self .default_slot = "default"
6664
6765 @override
6866 def render (self , context : Context ) -> SafeString :
@@ -81,40 +79,10 @@ def get_component_name(self, context: Context) -> str:
8179
8280 def get_component_context_data (self , context : Context ) -> dict [str , Any ]:
8381 attrs = Attrs .parse (self .attrs , context )
84- rendered_slots = self . render_slots ( context )
85- default_slot = rendered_slots .get (self . default_slot ) or context .get ("slot" )
82+ slots = Slots . collect ( self . nodelist , context ). render ( )
83+ default_slot = slots .get (DEFAULT_SLOT ) or context .get ("slot" )
8684 return {
8785 "attrs" : attrs .flatten (),
88- "slot" : mark_safe (default_slot ),
89- "slots" : {
90- name : mark_safe (content ) for name , content in rendered_slots .items ()
91- },
92- }
93-
94- def render_slots (self , context : Context ) -> dict [str , str ]:
95- if self .nodelist is None :
96- return {}
97-
98- contents : dict [str , list [str ]] = {self .default_slot : []}
99- active_slot = self .default_slot
100-
101- for node in self .nodelist :
102- if isinstance (node , SlotNode ):
103- active_slot = node .name
104- contents .setdefault (active_slot , [])
105- else :
106- active_slot = self .default_slot
107-
108- rendered_content = node .render (context )
109- contents [active_slot ].append (rendered_content )
110-
111- if (
112- all (not content for content in contents [self .default_slot ])
113- and "slot" in context
114- ):
115- contents [self .default_slot ] = [context ["slot" ]]
116-
117- return {
118- slot : template .Template ("" .join (content )).render (context )
119- for slot , content in contents .items ()
86+ "slot" : default_slot ,
87+ "slots" : slots ,
12088 }
0 commit comments