Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How do I extend Smarty to add some wrapping to all templates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
RQuadling
Smarty n00b


Joined: 08 Jul 2013
Posts: 2

PostPosted: Thu Dec 12, 2013 4:09 pm    Post subject: How do I extend Smarty to add some wrapping to all templates Reply with quote

Hi.

I would like to be able to add an HTML comment header/footer to every template and sub template so that when the source is viewed, we can see which template was used for the specific content.

Code:

<!-- [START] /templates/main/block/common/header.tpl -->
header.tpl's content is here.
<!-- [STOP] /templates/main/block/common/header.tpl -->
<!-- [START] /templates/main/block/common/body.tpl -->
body.tpl's content is here.
<!-- [START] /templates/main/block/common/loop.tpl -->
loop iteration
<!-- [STOP] /templates/main/block/common/loop.tpl -->
<!-- [START] /templates/main/block/common/loop.tpl -->
loop iteration
<!-- [STOP] /templates/main/block/common/loop.tpl -->
<!-- [START] /templates/main/block/common/loop.tpl -->
loop iteration
<!-- [STOP] /templates/main/block/common/loop.tpl -->
<!-- [START] /templates/main/block/common/loop.tpl -->
loop iteration
<!-- [STOP] /templates/main/block/common/loop.tpl -->
<!-- [STOP] /templates/main/block/common/body.tpl -->
<!-- [START] /templates/main/block/common/footer.tpl -->
footer.tpl's content is here.
<!-- [STOP] /templates/main/block/common/footer.tpl -->


sort of thing.

We have multiple portals with multiple blocks that override the default blocks if they exist and some of the newer devs are getting lost, so we think this would help us considerably.

Ideally, without amending anything within Smarty itself.

So, subclassing of Smarty or wrapping a standard plugin.

All of our Smarty content is for HTML, so we are happy that this would be applied to ALL templates.

Any suggestions, comments, etc. gratefully received.

Thank you.

Richard.
Back to top
View user's profile Send private message
RQuadling
Smarty n00b


Joined: 08 Jul 2013
Posts: 2

PostPosted: Tue Dec 17, 2013 11:53 am    Post subject: Reply with quote

Found it!

Add a prefilter using setAutoloadFilters() in my Smarty superclass.

The prefilter looks like ...

Code:
<?php

/**
 * Smarty plugin
 *
 * Wrap non live output with HTML tags to show the beginning and ending of the template.
 *
 * @package Smarty
 * @subpackage PluginsFunction
 */
function Smarty_prefilter_wrapWithSource($tpl_source, Smarty_Internal_Template $template)
{
    if (!Site::isLive()){
        $tpl_source = "<!-- BEGIN : {$template->_current_file} -->" . PHP_EOL
        . "{$tpl_source}" . PHP_EOL
        . "<!-- END : {$template->_current_file} -->";
    }

    return $tpl_source;
}


This is working very well. Ideally it would be nice to know the type of content of the tpl_source so that we can use the appropriate comment type, but at the moment, all the templates are HTML, so <!-- --> are fine.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP