#!/usr/bin/env python
# coding: utf-8
# In[1]:
import sys
sys.path.insert(0, '..')
from branca.element import *
# ## Element
# This is the base brick of `branca`. You can create an `Element` in providing a template string:
# In[2]:
e = Element("This is fancy text")
# Each element has an attribute `_name` and a unique `_id`. You also have a method `get_name` to get a unique string representation of the element.
# In[3]:
print(e._name, e._id)
print(e.get_name())
# You can render an `Element` using the method `render`:
# In[4]:
e.render()
# In the template, you can use keyword `this` for accessing the object itself ; and the keyword `kwargs` for accessing any keyword argument provided in the `render` method:
# In[5]:
e = Element("Hello {{kwargs['you']}}, my name is `{{this.get_name()}}`.")
e.render(you='World')
# Well, this is not really cool for now. What makes elements useful lies in the fact that you can create trees out of them. To do so, you can either use the method `add_child` or the method `add_to`.
# In[6]:
child = Element('This is the child.')
parent = Element('This is the parent.').add_child(child)
parent = Element('This is the parent.')
child = Element('This is the child.').add_to(parent)
# Now in the example above, embedding the one in the other does not change anything.
# In[7]:
print(parent.render(), child.render())
# But you can use the tree structure in the template.
# In[8]:
parent = Element("