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 to get all the name of the block in my template

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
jovenshadow11
Smarty Rookie


Joined: 28 Jun 2011
Posts: 14

PostPosted: Wed Jul 13, 2011 10:57 am    Post subject: how to get all the name of the block in my template Reply with quote

I have this project CMS and i want to create what other CMS are doing. i wan to create global content block.

let say you will create {block name=testBlock}

and from my template they will just use this code "{block name=testBlock}"

now i want to get all the name of the block inside my template and feed back the content on each block.

please help...
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Jul 13, 2011 4:12 pm    Post subject: Reply with quote

Template inheritance is a compile time process where templates which have a parent / child relationship defined by the {extends} tag or extends resource could supersede block definition.

I'm not sure if this is what you are looking for.

What did you mean by "assign the content"?
Back to top
View user's profile Send private message
jovenshadow11
Smarty Rookie


Joined: 28 Jun 2011
Posts: 14

PostPosted: Thu Jul 14, 2011 6:08 am    Post subject: Reply with quote

Hi U.Tews!

here is my code,

<?
require('plugins/Smarty-3.0.8/libs/Smarty.class.php');


function db_get_template ($tpl_name, &$tpl_source, $smarty_obj)
{

$tpl_source = "Hello {block name=yourName} welcome to my {block name=yourSite}";
// return true on success, false to generate failure notification
return true;
}

function db_get_timestamp($tpl_name, &$tpl_timestamp, $smarty_obj)
{

$tpl_timestamp = time();// this example will always recompile!
// return true on success, false to generate failure notification
return true;
}

function db_get_secure($tpl_name, $smarty_obj)
{
// assume all templates are secure
return true;
}

function db_get_trusted($tpl_name, $smarty_obj)
{
// not used for templates
}

$smarty = new Smarty();
$smarty->allow_php_templates= true;
$smarty->allow_php_tag = true;
$smarty->force_compile = false;
$smarty->debugging = false;
$smarty->caching = false;
$smarty->cache_lifetime = 100;

// register the resource name "db"
$smarty->registerResource("db", array("db_get_template","db_get_timestamp","db_get_secure","db_get_trusted"));

/*
Now i need to create program here that will collect all the name of the {block} i used and pass the value.

the {block data} are save in the database and on each page i used different block name, so each page they have different block data.

i want to create similar to CMS Made Easy where you can save {global_content name='sample'} in your database and just copy that code in your template

BTW this script in my index.php that will take care on all my pages.
*/


// using resource from php script
$smarty->display("db:index.tpl");
?>


again if you know how to do this please help or give me idea how will i do it. thanks!
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Thu Jul 14, 2011 6:43 am    Post subject: Reply with quote

Code:
$tpl_source = "Hello {block name=yourName} welcome to my {block name=yourSite}";


1) {block}s are blocks, not functions, so you need a closing tag for each block: {block name=yourName}{/block}
2) I don't see why you can't use variables here:
Code:
$tpl_source = "Hello {$yourName} welcome to my {$yourSite}";


are you caching the output and need to make sure that those variables aren't cached? {$foo nocache} might help you with that. Other than that, I really don't see how you even came up with that block approach…
Back to top
View user's profile Send private message Visit poster's website
jovenshadow11
Smarty Rookie


Joined: 28 Jun 2011
Posts: 14

PostPosted: Thu Jul 14, 2011 7:10 am    Post subject: Reply with quote

Hi Globe, thanks for the reply.

im not really sure why i used {block}, i've been using CMS Made Easy for a year now and i always used this block, so now my project is to create my own CMS for my project.

so let say im going to use {$yourname} and the data of this variable is on my database.

i can easily pass the data by using this

$data = "data collected from the database";
$smarty->assign('yourname',$data);

my problem is not all my pages has this variable, some may have {$anotherVar} and this variable has data in the database.

my question is, how can i collect all this {$variableName} in my template and create a script that will assign the value on the collected variable from my template. i pull the data from the database and pass in the variable in the template.

i know using assign but if you only know what is the variable name, but my pages has different variable on it on each pages.

let say,

About Page i used {$varForAboutPage}

Contact Page i used {$varForContactPage}

now the script above this thread is the script i used for showing this pages, i cannot put (script below)

$smarty->assign('varForAboutPage',"dataForAbout");
$smarty->assign('varForAboutPage',"dataForContact");
... and more of this because i have more pages...

you get what i mean? i need a script that will collect all the variable used in the page so that i will get only the right data from the database and show in the page.

sorry i have poor english... Sad if you know script that match my query pls forward to me.. thanks!
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Thu Jul 14, 2011 7:42 am    Post subject: Reply with quote

At the moment Smarty does not provide a reliable interface for collecting "used elements from a template" (such as variables, functions, modifiers, …). Put your way: you can't extract which data the template needs.

There are plans to introduce a default_variable_handler with Smarty 3.2. That might allow you to do what you want to do here. But, 3.1 is on the verge of being released and we haven't begun active development on 3.2 yet. (A bunch of tests and Proof of concepts, yes, but nothing nearly ready for deployment).

Your application design approach is not taken very often. Usually it's the app (php) defining what data has to be processed by the view (template). Not the other way 'round. That said, I did deploy Smarty based systems with a pull-strategy in the past. Here are two ideas for you:

1) use a pre-filter to scan and collect {$variables} from a template, store them beside the template (some cache file, or database). That way your application has a list of variables and can pull them from DB in one run before the template is executed.
Good: performance, agility.
Bad: need to "parse" templates with RegEx

2) {$variable} becomes {$FooBar:variable}. This translates to the Stream FooBar://variable which you can handle with PHP's StreamWrapper.
See http://www.smarty.net/docs/en/advanced.features.streams.tpl
and http://php.net/streams and an example http://php.net/manual/en/stream.streamwrapper.example-1.php
Good: agility
Bad: every variable would lead to its own db request - proper caching would be mandatory here.

{$FooBar:variable} is easer to RegEx for. The prefilter could prepend {loadMyFooBar vars="… collected thru regex …"} to the template. that way a single db request can fetch all needed data and assign it to the template directly, in which case the pre filter would convert {$FooBar:variable} to {$variable} or better yet {$FooBar.variable}).
Back to top
View user's profile Send private message Visit poster's website
jovenshadow11
Smarty Rookie


Joined: 28 Jun 2011
Posts: 14

PostPosted: Thu Jul 14, 2011 7:50 am    Post subject: Reply with quote

Thanks Globe for y our quick reply!

i think i will try the option 1 you mention as using regex in php and save all the variable in the database.

as for option 2 i will give it a shot also once i have the option 1 running.

this is a big help for me as im developing my inhouse CMS i will be asking more question from you guys....

if that function will be included in the 3.2 version i think that will be a good help for developers...

again thank you...
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 -> Smarty 3 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