Dynamic pages

### Navigation

- index
- next |
- previous |
- fantastico 0.5.0-b126 documentation »
- Fantastico features »
- Built in components »

# Dynamic pages

Most of the time, when a developer create a new web site or web application he follows the steps:

> 1. Create a set of templates (can be delegated to a web designer)
> 2. Create a new API or create a proxy API.
> 3. Create pages over the templates.

Many web sites / applications have a minimal set of master templates and all web pages follow those templates. This kind of approach keeps site consistency and decouple layouts from actual content. In Fantastico, it is extremely easy to work in this manner thanks to Dynamic pages extension.

Dynamic pages divides pages into two main parts:

> 1. Page meta information (title, keywords, description, language)
> 2. Page model / content (markup, text keys or any other kind of information).

Using dynamic pages you can easily add new web pages to your project without writing a single line of server side code.

## Integration

1. Activate Dynamic pages extension.

       fsdk activate-extension --name dynamic_pages --comp-root <comp_root>

2. Add new dynamic pages to your project using <comp_root>/sql/create_data.sql.

       INSERT INTO pages(id, name, url, template, keywords, description, title, language)
       VALUES(1, '/en/home', '/en/home', '/frontend/views/master.html', 'keyword 1, ...', 'Home page', 'description', 'en-US');

       INSERT INTO page_models(page_id, name, value)
       VALUES(1, 'article_left', '<p class="hello_world">Hello world.</p>');

       INSERT INTO page_models(page_id, name, value)
       VALUES(1, 'article_right', '<p class="hello_world_right">Hello world right.</p>');

3. Update your project database

       fsdk syncdb --db-command /usr/bin/mysql --comp-root <comp_root>

4. Create master.html template file under <comp_root>/frontend/views/.

       <!DOCTYPE html>

       <html lang="{{page.language}}">
          <head>
             <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
             <meta name="viewport" content="width=device-width, initial-scale=1.0">
             <meta name="keywords" content="{{page.keywords}}" />
             <meta name="description" content="{{page.description}}" />

             <title>{{page.title}}</title>
          </head>

          <body>
             <h1>{{page.article_left.value}}</h1>

             <h2>{{page.article_right.value}}</h2>
          </body>
        </html>

After you integrated dynamic pages extension into your project you can access http://localhost:12000/dynamic/test/default/page from a browser. You should see a very simple dynamic page rendered.

## Current limitations

In the first version of this component (part of Fantastico 0.4) there are some known limitations:

> - Create / Delete / Update / Bulk listing API are not provided. You can do this through create_data.sql script.
> - There is no way to rewrite dynamic pages url so that they do not contain /dynamic prefix.

## Technical summary

class `fantastico.contrib.dynamic_pages.pages_router.` `PagesRouter`(settings_facade)

  This class provides the API for managing dynamic pages. In addition, it creates the special route /dynamic/<page_url> used to access pages stored in the database. From dynamic pages module perspective, a web page is nothing more than a relation between `fantastico.contrib.dynamic_pages.models.pages.DynamicPage` and `fantastico.contrib.dynamic_pages.models.pages.DynamicPageModel`.

  A typical template for dynamic pages might look like:

      <!DOCTYPE html>

      <html lang="{{page.language}}">
         <head>
            <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta name="keywords" content="{{page.keywords}}" />
            <meta name="description" content="{{page.description}}" />

            <link href="/frontend/static/css/bootstrap-responsive.css" rel="stylesheet">
            <link href="/frontend/static/css/forhidraulic.css" rel="stylesheet">
            <title>{{page.title}}</title>
         </head>

         <body>
            <h1>{{page.article_left.value}}</h1>

            <h2>{{page.article_right.value}}</h1>
         </body>
       </html>

  `serve_dynamic_page`(*args, **kwargs)

    This method is used to route all /dynamic/... requests to database pages. It renders the configured template into database binded to `fantastico.contrib.models.pages.DynamicPageModel` values.

class `fantastico.contrib.dynamic_pages.models.pages.` `DynamicPage`(name=None, url=None, template=None, keywords=None, description=None, title=None, language='en')

  This model holds meta information about dynamic pages. Below you can find all meta information for a dynamic page:

  > 1. id (unique identifier for a dynamic page)
  > 2. name
  > 3. url
  > 4. template
  > 5. keywords
  > 6. description
  > 7. language

  In a template used for rendering dynamic pages, you can easily access page meta information:

      <p>Id: {{page.id}}</p>
      <p>Name: {{page.name}}</p>
      <p>Url: {{page.url}}</p>
      <p>Template: {{page.template}}</p>
      <p>Keywords: {{page.keywords}}</p>
      <p>Description: {{page.description}}</p>
      <p>Language: {{page.language}}</p>

  Usually it does not make sense to display dynamic page unique identifier but you can do it if necessary.

class `fantastico.contrib.dynamic_pages.models.pages.` `DynamicPageModel`(page_id=None, name=None, value=None)

  This class defines how page models looks like. A page model defines the actual content for en existing page.