webERP Forum
Views: Twig or no Twig? - Printable Version

+- webERP Forum (http://www.weberp.org/forum)
+-- Forum: webERP Discussion (/forumdisplay.php?fid=1)
+--- Forum: Development Discussion & Specification (/forumdisplay.php?fid=10)
+--- Thread: Views: Twig or no Twig? (/showthread.php?tid=2149)



Views: Twig or no Twig? - serakfalcon - 03-11-2014 02:22 AM

twig:
Code:
<div class="table-responsive">
<table class="table table-striped table-hover {{ classes }}" {{ attributes }} >
{% if title %}
    <caption class="text-left">
        <h4>
            {{ title }}
        </h4>
    </caption>
{% endif %}

{% if headers|length > 0 %}
    <!-- header row -->
    <thead>
            <tr>
                
                {% for header in headers %}
                <th {% if header.class %} class="{{ header.class }}" {% endif %}{% if header.span %} colspan="{{ header.span }}"{% endif %} >
                    {{ header.content|e }}
                </th>
                {% endfor %}
                {% if columnCount > headerCount %}
                    {% for i in range(columnCount,headerCount) %}
                        <th class="no-sort"> </th>
                    {% endfor %}
                {% endif %}
            </tr>
    </thead>
{% endif %}
    <!-- table content -->
    <tbody>
        {% spaceless %}
        {% for row in rows %}
            <tr {% if row.htmlclass %}class="{{ row.htmlclass }}"{% endif %} {{ row.attributes }}>
                {% for column in row.columns %}
                        <t{{ column.isheader ? 'h' : 'd'}} {% if column.span >1 %} colspan="{{ column.span }}" {% endif %} {% if column.class %} class="{{ column.class }}"{% endif %}>
                            {% if column.link %}
                                <a href="{{ column.link }}" {{ column.attributes }} >
                                    {{ column.content|e }}
                                </a>
                            {% else %}
                                {{ column.content|e }}
                            {% endif %}
                        </t{{ column.isheader ? 'h' : 'd'}}>
                {% endfor %}
            </tr>
        {% endfor %}
        {% endspaceless %}
    </tbody>
</table>
</div>

No twig:

Code:
<div class="table-responsive">
<table class="table table-striped table-hover <?php echo $this->classes['table'];?>" <?php echo $this->attributes; ?>>
<?php
if(!empty($this->title)) { ?>
    <caption class="text-left">
        <h4>
            <?php echo $this->title; ?>
        </h4>
    </tr>
<?php
} //end title statement
if(!empty($this->headers)) { ?>
    <!-- header row -->
    <thead>
        <?php
        if (!empty($this->headers)) { ?>
            <tr>
                <?php
                foreach ($this->headers as $columnhead) {
                    echo '<th';
                    if ($columnhead['class']) {
                        echo ' class="' . $columnhead['class'] . '"';
                    }
                    if ($columnhead['span'] > 1) {
                        echo ' colspan="' . $columnhead['span'] . '"';
                    }
                    echo '>';
                    echo $columnhead['content']; ?>
                    </th>
                    <?php
                }
                ?><!-- <?php echo $this->columnCount . ' vs. ' . $this->headerCount; ?> --><?php
                if ($this->columnCount > $this->headerCount) {
                    $i = $this->headerCount;
                    while ($i < $this->columnCount) {
                        echo '<th class="no-sort"> </th>';
                        $i++;
                    }
                }
                ?>
            </tr>
        <?php } // end headers statement ?>
    </thead>
<?php
} // end thead if statement ?>
    <!-- table content -->
    <tbody>
        <?php
        foreach ($this->rows as $row) {
            ?>
            <tr <?php echo $row->htmlclass ? 'class="' . $row->htmlclass . '"' : '';
                      echo $row->attributes ? ' ' . $row->attributes : '';
            ?>>
                <?php
                foreach ($row->columns as $column) {
                    if ($column['isheader']) {
                        echo '<th';
                    } else {
                        echo '<td';
                    }
                    if ($column['span'] != 1) {
                        echo ' colspan="' . $column['span'] . '"';
                    }
                    if ($column['class']) {
                        echo ' class="' . $column['class'] . '"';
                    }
                    echo '>';
                    if ($column['link']) {
                        if ($column['attributes']) {
                            echo '<a href="' . $column['link'] . '" ' . $column['attributes'] . '>' . $column['content'] . '</a>';
                        } else {
                            echo '<a href="' . $column['link'] . '">' . $column['content'] . '</a>';
                        }
                    } else {
                        echo $column['content'];
                    }
                    if ($column['isheader']) {
                        echo '</th>';
                    } else {
                        echo '</td>';
                    }
                }
                ?>
            </tr>
            <?php
        }
        ?>
    </tbody>
</table>
</div>

Which one is better/WebERPier?


RE: Views: Twig or no Twig? - Forums - 03-11-2014 03:03 AM

No twig is better and definitely more webERPier :-)


RE: Views: Twig or no Twig? - agaluski - 03-11-2014 03:17 AM

I agree that not introducing new syntax and language concepts is better for WebERP and for users trying to modify the scripts


RE: Views: Twig or no Twig? - serakfalcon - 03-11-2014 12:56 PM

Yeah I had a little headache figuring out how to make it work and I think I like sticking to php better- the syntax is a bit annoying but there is a ton more documentation available for every single php problem, while the documentation for twig is good for about 80% it is missing a lot of information and it's not as easy to find help. However in the end it's quite simple to include into the views module I'm developing so I may make a twig theme just to show what is possible.


RE: Views: Twig or no Twig? - icedlava - 03-11-2014 02:15 PM

I found twig took a short while to get used to - but much better than any of the other heavy weight templating engines I've used.

One would expect to have variables embedded in the HTML in twig because that is what template systems do - they allow you to separate out HTML from the PHP code and thus provide a basis for much easier templating of themes. Theme creaters in general do not need to know PHP. Rendering of output is separate from code.

On the plus side too, Twig is very easy to integrate to webERP - incredibly so and comes with a nice caching system. Initial results i had demonstrate performance improvement. Other benefits are easy PDF output etc.

The best part is one can just change the view without having to touch the php code files.

PHP has changed a lot over the years since initial versions, as have templating systems, caching options and so on. It takes some time to throw off old habits and learn new ones - sometimes it's worthwhlle and in the case of TWIG i've found it to be so.

Cheers,

I forgot to add that the code files in webERP - once the HTML output is removed, are very much smaller and easier to read (at least for me). I also found i could remove a lot of duplicate code. That's another plus I guess.
One of the things about TWIG is that it does force you to relook at the code and what variables you need to pass to the output. The idea I've kept in mind is to attempt to reduce any use of PHP or non HTML code in the templates when deciding what variables to pass to twig output.

For those interested a quick and dirty integration with webERP goes like so (been a while since i did this so i'm looking at my rough notes from months ago) - it was just to test it out.

1. Create a directory called 'twig' in the includes directory of webERP.
2. Download twig files and put these files in the new includes/twig directory
3. Create a directory 'output' in the html directory writable by your web server - this is where templates can be stored
4. Create a directory 'tmp' in the html directory writeable by your webserver - this is where cache output is stored
5. I made a new includes/header.php and includes/footer.php file to take the place of header.inc and footer.inc. You could just use the existing header.inc and footer.inc with changes but the new header.php and footer.php add some flexibility.
In the new header.php the key lines are:
Code:
//template system loaded in the header file
    require_once './includes/twig/lib/Twig/Autoloader.php';
    Twig_Autoloader::register();
    $loader = new Twig_Loader_Filesystem(array('./output/templates','./output/templates/includes'));
    $twig = new Twig_Environment($loader, array(
            'cache' => './tmp/cache',
            'debug' => true,
            'auto_reload' => true
    ));
    //template system done
You could then also add here an array containing all the variables you want to output to the header area in the templates eg
Code:
$params =
        array(
    'StrictXHTML'     => $StrictXHTML,
    'SessionTheme'    => $_SESSION['Theme'],
    'SessionLanguage' => $_SESSION['Language',
...
etc,
'Logout' => _('Logout')
);
In the new footer all I have are the parameters to send to the template
Code:
$params = array_merge ($params, array(
    'version' => _('version'),
    'versionNo' =>  $_SESSION['VersionNumber'],
    'copyright' =>  _('Copyright'),
    'year'      => Date('Y'),
    'logofile' => $_SESSION['LogoFile'],
    'RootPath' => $RootPath,
    'datetime' => DisplayDateTime()
    )

6. Create your templates and put them in the directory specified in point 5 above which is in my case output/templates or output/templates/includes if i template one of the include files.

7. In each webERP script file, make sure to include header.php and footer.php, capture the variables to send to the template in an array, and send to the template:
Code:
$params = array_merge($params, array(
   'StatusMsg' => $statusMsg,
   'RootPath'   => $RootPath,
....
   'assets' => $assets,
   )
);

//set the template to use and output!
    $template = $twig->loadTemplate('FixedAssetCategories.phtml');
    $template->display($params);

I used array_merge above - because i had a $params previously defined in header template. We could architecture a better integration but this was nice and quick for a look see.

That's about it for a quick and dirty integration with caching.

I might have forgotten something but serakfalcon could let me know as he's working with it now Smile

I started templating some simple files to get started and moved on to more complex ones. As I went I found I could tidy up the webERP code and ensure that as much processing is done before output to the templates - this then minimizes the processing there and keeps the HTML looking more like HTML, easier for themers too!

Cheers,


PS. thanks to serakfalcon for the TWIG thread - good to have somewhere to discuss it!