Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

make section a plugin

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Mon May 05, 2003 2:25 pm    Post subject: make section a plugin Reply with quote

To make smarty super extendable, and super flexable, it would be nice to make things like section a plugin.

I would like to be able to make plugins that act like {section} and having section as a plugin would be the first step.

then, if you make a plugin based on the section plugin, you can have super cool functions :

{users country="NZ"}
{$users.name}<br>
{/users}

( notice how it's not {$users[n].name} .. I think the [n] is annoying and unneccery in lots of situations, sure it's needed in the php code, but not in the smarty code. It would be nice if in the section plugin "name=n" was optional )
Back to top
View user's profile Send private message Visit poster's website
aloner
Smarty Rookie


Joined: 24 Apr 2003
Posts: 24

PostPosted: Mon May 05, 2003 3:05 pm    Post subject: Reply with quote

You can do it quite easy with prefilter.
_________________
Your ad here.
Back to top
View user's profile Send private message
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Tue May 06, 2003 1:24 am    Post subject: Reply with quote

aloner wrote:
You can do it quite easy with prefilter.


if you're talking about enabling {$users.name} to work, sure.

but I don't see how a prefilter would help in making the section, a plugin.
Back to top
View user's profile Send private message Visit poster's website
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Sun May 11, 2003 4:24 pm    Post subject: Reply with quote

just to elaborate....


Smarty is great. It is so simple yet so powerful, and enables clean php sites due to seperating the logic from the display. You can do some fancy tricks too, I won't go over them here.. But there is one way to use smarty that I think is the most useful way to use it as part of a larger project. Say I create a blogging system. I want people to be able to easily edit templates, and easily add plugins. solution : smarty.

But it's not easy enough to do loops while having full control over the display in a clean manner.

for example : ( plugin : function.links.php )
{links section='blogroll'} could return a whole bunch of links seperated by a line break. But what if you wanted it as a dropdown list? or thumbs of the site?

you could go :
{links section='blogroll' assign=$links}

{section name=l loop=$links}
<a href={$links[l].url}>$links[l].name</a>
{section}

there are 2 problems with that.
here is how it should work :

{links section='blogroll'}
<a href={$links.url}>{$links.name}</a>
{/links}

the difference being that you can have a looping block ( you can do this with current smarty CVS ) that acts like a section ( not in smarty cvs yet ). The other thing is removing the name/index from the looping varible. I never use it. it's a waste of space.

Now, the point of all that? Well you can make a plugin that goes to the db and gets your links, in one file called block.links.php and it's available to put in your template straight away, with 2 tags that act like a section and block combined.


PLEASE someone back me up and say this is a good idea. If you are writing a GPL CMS you'll know where I'm coming from.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 11, 2003 11:32 pm    Post subject: Reply with quote

eadz, I'm not 100% in love with the current sections implementation (though it ain't too shabby) but I can't say that I back you're reasoning on this one. The CVS block functionality is essentially everything that you describe. Even the parts of what you describe that I don't like (see below) are doable with the CVS blocks implementation.

One thing I don't like about your example is the "magic" names that the variables take. The $links array is automatically assigned? Not nice. Heck, why even have it assign to an array called $links? Just assign $url, $name, etc. Right, it will likely cause name collissions in the template. That's the problem with auto-naming--it bites people in the rear.

If you don't like the looping var, perhaps you should take a closer look at foreach ?

Quote:
The other thing is removing the name/index from the looping varible. I never use it. it's a waste of space.


Yes, you do use it, otherwise you aren't using {sections} -- they're required !

Sorry eadz!!
Back to top
View user's profile Send private message
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Mon May 12, 2003 3:47 pm    Post subject: Reply with quote

boots wrote:
eadz, I'm not 100% in love with the current sections implementation (though it ain't too shabby) but I can't say that I back you're reasoning on this one. The CVS block functionality is essentially everything that you describe. Even the parts of what you describe that I don't like (see below) are doable with the CVS blocks implementation.


Yes, the current sections implimentation isn't too shabby, along with smarty it's excellent. I can tell my looking at the smarty code, that it is coded very very well, to be fast and secure. I have been programming php for 2 years, but smarty uses some "hardcore" oop stuff, which i strugle to get my head around ( otherwise I would add this functionality myself Smile

I have looked at the cvs implimentation, and I can see how to do loops, but not sections. Could you please give an example of a block plugin that loops over a multidimential array like section? I take it that is what you mean by it's doable in the current cvs. I would be forever greatful!


the idea being I have block.test.php , ( I have included the assign= because as you say not a good idea to auto name varibles ).
and in that function, it has an array defined :
Code:

$samplea = array(array("name" => "Car", "description"=>"fast"), array("name" => "Truck","description"=>"big"));
$sampleb = array(array("name" => "Plum", "description"=>"small and red"), array("name" => "lemon","description"=>"sour, yellow"));

and the template looks like :
Code:

{test sample=b assign=$test}
<P><b>$test.name</b> - $test.description
{/test}


Quote:

One thing I don't like about your example is the "magic" names that the variables take. The $links array is automatically assigned? Not nice. Heck, why even have it assign to an array called $links? Just assign $url, $name, etc. Right, it will likely cause name collissions in the template. That's the problem with auto-naming--it bites people in the rear.


Yes you're right, not a great idea for smarty as a rule, but it could be useful in some cases on particular sites when you are aware of what's happening.


Quote:

Quote:
The other thing is removing the name/index from the looping varible. I never use it. it's a waste of space.


Yes, you do use it, otherwise you aren't using {sections} -- they're required !


What i meant was i don't ever "use" that name paramater for anything. I just make up a name e.g. name=n and that's it.

Best Regards
Eaden
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon May 12, 2003 7:06 pm    Post subject: Reply with quote

eadz wrote:
I have looked at the cvs implimentation, and I can see how to do loops, but not sections. Could you please give an example of a block plugin that loops over a multidimential array like section? I take it that is what you mean by it's doable in the current cvs.


i hope i understand your question right and i had to modify your example slightly. here it goes (i was too lazy for proper parameter-checking and stuff, so this is not to be seen as a turn-key-solution Smile ):

block.test.php
Code:
<?php

function smarty_block_test($params, $content, &$smarty, &$repeat) {
    static $froms = array();

    if (!isset($content)) {
        /* start loop */
        $froms[$params['assign']] = $params['from'];
        reset($froms[$params['assign']]);

    } else {
        /* next iteration */
        next($froms[$params['assign']]);

    }

    /* get current array-key */
    $key = key($froms[$params['assign']]);

    /* set $repeat to false at the end of the loop, to true otherwise */
    $repeat = $key !== null;

    if ($repeat) {
        /* assign loop variable */
        $smarty->assign($params['assign'], $froms[$params['assign']][$key]);       
    } else {
        /* free static variable */
        unset($froms[$params['assign']]);
    }


    return $content;
}

?>


then in php do:
Code:

$samplea = array(array("name" => "Car", "description"=>"fast"), array("name" => "Truck","description"=>"big"));
$sampleb = array(array("name" => "Plum", "description"=>"small and red"), array("name" => "lemon","description"=>"sour, yellow"));

$smarty->assign('a', $samplea);
$smarty->assign('b', $sampleb);


and in the template do
Code:
{test from=$a assign=test}
<P><b>{$test.name}</b> - {$test.description}
{/test}


{test from=$b assign=test}
<P><b>{$test.name}</b> - {$test.description}
{/test}


you can even nest {test}...{/test} blocks if you choose distinct variable-names to assign to. note that "assign" is passed by name, but "from" is passed by value.

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Mon May 12, 2003 11:44 pm    Post subject: Reply with quote

Thankyou so much!!!

This is great!

Very Happy Smile Laughing Wink Razz Cool Laughing Sad Smile Very Happy Surprised Laughing
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests 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