Component reusage

### Navigation

- index
- next |
- previous |
- fantastico 0.4.0-b114 documentation »
- Fantastico features »

# Component reusage

class `fantastico.rendering.component.` `Component`(environment, url_invoker_cls=<class 'fantastico.rendering.url_invoker.FantasticoUrlInternalInvoker'>)

  In fantastico, components are defined as a collection of classes and scripts grouped together as described in Component model. Each fantastico component provides one or more public routes that can be accessed from a browser or from other components. This class provides the mechanism for internal component referencing.

  In order to gain a better understanding about internal / in process component referencing we assume Blog component provides the following public routes:

  > - /blog/articles/<article_id> - Retrieves information about an article.
  > - /blog/ui/articles/<article_id> - Displays an article within a html container.

  The first url is a simple json endpoint while the second url is a simple html dynamic page. When we want to reuse a datasource or an dynamic html page in fantastico is extremely easy to achieve. Lets first see possible responses from the above mentioned endpoints:

      /* /blog/articles/<article_id> response */
      {"id": 1,
       "title": "Simple blog article",
       "content": "This is a simple and easy to read blog article."}

      <!-- /blog/ui/articles/<article_id> response-->

      <div class="blog-article">
          <p class="title">Simple blog article</p>

          <p class="content">This is a simple and easy to read blog article.</p>
      </div>

  A very common scenario is to create multiple views for a given endpoint.

      <!-- web service server side reusage -->
      {% component url="/blog/articles/1", template="/show_blog_formatted.html", runtime="server" %}{% endcomponent %}

      <!-- show_blog_formatted.html -->
      <p class="blog-title">{{model.title}}</p>
      <p class="blog-content">{{model.content}}</p>

  As you can see, json response is plugged into a given template name. It is mandatory that the given template exists on the component root path.

  Also a very common scenario is to include an endpoint that renders partial html into a page:

      <!-- html server side reusage -->
      {% component url="/blog/ui/articles/1",runtime="server" %}{% endcomponent %}

  Runtime attribute is used for telling Fantastico if the rendering needs to take place on server side or on client. Currently, only server side rendering is supported which actually means a page will be completed rendered on server and then the markup is sent to the browser.

  In order to reduce required attributes for component tag, runtime attribute is optional with server as default value.

  `parse`(parser)

    This method is used to parse the component extension from template, identify named parameters and render it.

    Parameters: | parser (Jinja 2 parser.) – The Jinja 2 expression parser.
    Returns: | A callblock able to render the component.
    Raises FantasticoInsufficientArgumentsError:
    | when no / not enough arguments are provided to component.

  `render`(template='/raw_dump.html', url=None, runtime='server', caller=<function <lambda> at 0x2a991e8>)

    This method is used to render the specified url using the given parameters.

    Parameters: | template (string) – The template we want to render into the result of the url. url (string) – The url we want to invoke. runtime (string) – The runtime we execute the rendering into. Only server is supported for now. caller (macro) – The caller macro that can retrieve the body of the tag when invoked.
    Returns: | The rendered component result.
    Raises: | fantastico.exceptions.FantasticoTemplateNotFoundError – Whenever we try to render a template which does not exist. fantastico.exceptions.FantasticoUrlInvokerError – Whenever an exception occurs invoking a url within the container.