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

help meeeeeeeeeeeeee

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Bamboo
Smarty Rookie


Joined: 06 Aug 2003
Posts: 7

PostPosted: Thu Aug 07, 2003 11:10 am    Post subject: help meeeeeeeeeeeeee Reply with quote

Sorry my english. I'm new in Smarty and not familiar with PHP. help me plz...

I have two section in "verticalmenu.conf":

[name]
name1 = Job1
name2 = Job2
name3 = Job3

[url]
url1 = ./jobs/job1.html
url2 = ./jobs/job2.html
url3 = ./jobs/job3.html

And i want OUTPUT the same:

<table>
<tr>
<td><a href="./jobs/job1.html">Job1</a></td>
<td><a href="./jobs/job2.html">Job2</a></td>
<td><a href="./jobs/job3.html">Job3</a></td>
</tr>
</table>

But i don't know how to use "section" or "foreach" to loop item in section.
And, how to get data from .conf in PHP ???

Help me.........
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Thu Aug 07, 2003 12:35 pm    Post subject: Reply with quote

Hi,
I'd suggest renaming the "keys" for [name] and [url] to match, so you can use them to reference each other
Quote:
[name]
link1 = Job1
link2 = Job2
link3 = Job3

[url]
link1 = ./jobs/job1.html
link2 = ./jobs/job2.html
link3 = ./jobs/job3.html

This simplifies access to each entry in the section and you avoid parsing, replacement or calculations.

The following is one possible approach to read config data with PHP and save the data into $smarty.

You first need to get an instance of the Config_File class, then tell it where the config files are, which should be available from $smarty->config_dir.
Next you call the get() method to load the desired sections from your file. This is similar to {config_load file="xxx" section="xxx"}, but if you do it within PHP, you don't need {config_load} in your templates though.
[php:1:c2d8b757aa]<?php
# You should have SMARTY_DIR defined and an instance of
# $smarty at this point, with the $cinfig_dir property pointing
# to something like "/htdocs/www/templates/configs"
# I assume you have some sort of "prepend file" to do this.

/* load Config_File class */
require_once SMARTY_DIR . '/Config_File.class.php';
$CfgFile = new Config_File($smarty->config_dir);

/**
* Retrieves config info based on the file, section, and variable name.
*
* @param string $file_name config file to get info for
* @param string $section_name (optional) section to get info for
* @param string $var_name (optional) variable to get info for
* @return string|array a value or array of values
*/
$vmCaptions =& $CfgFile->get('verticalmenu.conf', 'name');
$vmUrls =& $CfgFile->get('verticalmenu.conf', 'url');

$smarty->assign('vmCaptions', $vmCaptions);
$smarty->assign('vmUrls', $vmUrls);
$smarty->display('verticalmenu.tpl');
?>
[/php:1:c2d8b757aa]
Then with some template file like this (verticalmenu.tpl) in your $smarty->template_dir
Quote:
<html>
<body>
<h1>Vertical menu</h1>
<table>
{foreach from=$vmCaptions key=vmKey item=vmCaption}
<tr><!-- Key: $vmKey -->
<td><a href="{$vmUrls.$vmKey}">{$vmCaption}</a></td>
</tr>
{{/foreach}
</table>
</body>
</html>

$vmKey will contain the keyname from the $vmCaption associative array and if combined as {$vmUrls.$vmKey} it will become $vmUrls.link1, $vmUrls.link2 etc.

Ok, I hope I didn't screw anything up while copying the code.
If you have any trouble or questions, feel free to ask.
If you're uncomfortable with the directory settings of Smarty, pease consult the manual first. In general all paths must NOT contain a trailing slash and should be given as absolute paths.

Have fun,
CirTap
Back to top
View user's profile Send private message Visit poster's website
Bamboo
Smarty Rookie


Joined: 06 Aug 2003
Posts: 7

PostPosted: Fri Aug 08, 2003 10:34 am    Post subject: Reply with quote

I understood what you said.
thanks CirTap for your help.
Back to top
View user's profile Send private message
Bamboo
Smarty Rookie


Joined: 06 Aug 2003
Posts: 7

PostPosted: Sat Aug 09, 2003 10:07 am    Post subject: Reply with quote

H CirTap,
I did what ever you shown me. It's work great. And i tried to mime your code to make another array.

[img]
link1 = ./images/img1.gif
link2 = ./images/img2.gif
link3 = ./images/img3.gif

img1.gif is not the same as img2.gif and not the same as img3.gif.
I tried, but didn't know how to code PHP and tpl. I wanna OUTPUT to (little changes)

<table>
<tr>
<td backround="./images/img1.gif><a href="./jobs/job1.html">Job1</a></td>
<td backround="./images/img1.gif><a href="./jobs/job2.html">Job2</a></td>
<td backround="./images/img1.gif><a href="./jobs/job3.html">Job3</a></td>
</tr>
</table>

I tried many time to nest item (use {foreach} loop. But i didn't get the valid OUTPUT i want.

Help me to resolve it.
Thanks alot.

Best regard,
Bamboo.
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Sat Aug 09, 2003 2:06 pm    Post subject: Reply with quote

Hi,
you could have another array holding the images, Just add the two lines below their counterparts[php:1:96e55ba28b]<?php
... // reads the [img] section
$vmImgs =& $CfgFile->get('verticalmenu.conf', 'img');
... // add the new array to Smarty
$smarty->assign('vmImgs', $vmImgs);
?>[/php:1:96e55ba28b]
and your template will then look like this:
Quote:
<html>
<body>
<h1>Vertical menu</h1>
<table>
{foreach from=$vmCaptions key=vmKey item=vmCaption}
<tr><!-- Key: $vmKey -->
<td backround="{$vmImgs.$vmKey}"><a href="{$vmUrls.$vmKey}">{$vmCaption}</a></td>
</tr>
{/foreach}
</table>
</body>
</html>


There are certainly other ways to achive the same effects, but if you want to stick to the config files, this is pretty straight forward.
Just keep in mind, that the keys must always match in each section, and that elements in every additional array (like $vmImgs now) will be accessed via {$newArrayName.$sameKeyName}.

Whenever it happens that I duplicate some code for the third time, I try to find a more generic approach, so here's a solution that will go even further and let you have all in your config file.
This method will free you from changing the PHP code everytime you need another array/var combination. In example you may add "long titles" for the links to use them in some other page or as the title-attribute in the link. It's up to you.
Code:

# defines what sections go to what variable
# config-section = Smarty-variable
[mapping]
name = vmCaption
url = vmUrls
img = vmImgs

# the sections as used before
[name]
link1 = ...
...
[url]
link1 = ...
...
[img]
link1 = ...
...


Then you can read the [mapping] section first, which will tell the script later what to do - automagically Smile And if you have to change your templates to use other variables, you only have to update the config file.
[php:1:96e55ba28b]<?php
require_once SMARTY_DIR . '/Config_File.class.php';
$CfgFile = new Config_File($smarty->config_dir);

// find out about the mapping between section and variable
$cfgFile = 'verticalmenu.conf';
$mappings = $CfgFile->get($cfgFile, 'mapping');
// assign the keys from each $section to the corresponding $varname
foreach ($groups as $section => $varname) {
$smarty->assign($varname, $CfgFile->get($cfgFile, $section) );
}
?>[/php:1:96e55ba28b]
This is a little more abstract, and I have just hacked in in here, so there might be a typo. You can put this in some function so you can use it in other scripts and with other config files:
[php:1:96e55ba28b]<?php
function varAssign($cfgFile, &$CfgFile) {
// find out about the mapping between section and variable
$mappings = $CfgFile->get($cfgFile, 'mapping');
// assign the keys from each $section to the corresponding $varname
foreach ($groups as $section => $varname) {
$GLOBALS['smarty']->assign($varname, $CfgFile->get($cfgFile, $section) );
}
}
require_once SMARTY_DIR . '/Config_File.class.php';
$CfgFile = new Config_File($smarty->config_dir);
// blow up Smarty with all kind of vars Smile
varAssign('verticalmenu.conf', $CfgFile);
varAssign('submenu.conf', $CfgFile);
varAssign('gallery.conf', $CfgFile);
?>[/php:1:96e55ba28b]

If you have any trouble with this, let me know.

Have fun,
CirTap
Back to top
View user's profile Send private message Visit poster's website
Bamboo
Smarty Rookie


Joined: 06 Aug 2003
Posts: 7

PostPosted: Sun Aug 10, 2003 1:50 pm    Post subject: Reply with quote

I try to use your code. With this function:
[php:1:a91903ef2a]<?php

function varAssign($cfgFile, &$CfgFile) {
// find out about the mapping between section and variable
$mappings = $CfgFile->get($cfgFile, 'mapping');
// assign the keys from each $section to the corresponding $varname
foreach ($groups as $section => $varname) {
$GLOBALS['smarty']->assign($varname, $CfgFile->get($cfgFile, $section) );
}
}
?>[/php:1:a91903ef2a]

I met error with foreach(), invalid argument. I changed $groups to $mappings. Then I saw array of datas in verticalmenu.conf. But array of data didn't shown in browser... i couldn't see any link i was set in config file.

But I met another problem: I cann't find any background images associate with URL in .conf file. I put "images" folder in "templates" folder.

I have this:

Quote:
$smarty = assign('imgdir', $smarty->template_dir.'images');


now i don't know where to put "imgdir" ???[/quote]
_________________
Thanks for the help.

Best regard,
Bamboo.
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Wed Aug 13, 2003 3:26 pm    Post subject: Reply with quote

Hi,
mea culpa, but I told you there might be typos Smile And there were some in the previous code/template.
I finally write some script that works and corrected all the bugs.
Copy the .php file in some directry of your document_root, the .tpl goes to the $template_dir and the .conf in the $config_dir.
For the image path you should only specify absolute URLs to avoid trouble with relative paths inpages that come from a subdirectory, the template would then create <img src="/images/filename.gif" ... />
[php:1:bb1e3b9439]function varAssign($filename, &$CfgFile) {
// find out about the mapping between section and variable
$mappings = $CfgFile->get($filename, 'mapping');
// assign the keys from each $section to the corresponding $varname
foreach ($mappings as $section => $varname) {
$GLOBALS['smarty']->assign($varname, $CfgFile->get($filename, $section) );
}
}

require_once SMARTY_DIR . '/Config_File.class.php';
$CfgFile = new Config_File($smarty->config_dir);
// blow up Smarty with all kind of vars
varAssign('verticalmenu.conf', $CfgFile);

$smarty->display('verticalmenu.tpl');

# echo "<xmp>";
# print_r($smarty->_tpl_vars);
# echo "</xmp>";
[/php:1:bb1e3b9439]
Code:
# defines what sections go to what variable
# config-section = Smarty-variable
[mapping]
name = vmCaptions
url  = vmUrls
img  = vmImgs

# the sections as used before
[name]
link1 = HTML Area
link2 = CSS Area
link3 = XHTML Area

[url]
link1 = /html/index.html
link2 = /css/index.html
link3 = /xhtml/index.html

[img]
link1 = /images/buttons/vh40.gif
link2 = /images/buttons/vcss.gif
link3 = /images/buttons/valid-xhtml10.gif

Quote:
<h1>Vertical menu</h1>
<table border="1">
{foreach from=$vmCaptions key=vmKey item=vmCaption}
<tr>
<td><img src="{$vmImgs.$vmKey}"><a href="{$vmUrls.$vmKey}">{$vmCaption}</a></td>
</tr>
{/foreach}
</table>

The variable names are probably not a well choosen Smile try using something less error-prone
You may certainly use background={$vmImgs.$vmKey} instead of <img src="{$vmImgs.$vmKey}">
The results look like this:
Code:
<h1>Vertical menu</h1>
<table border="1">
<tr>
<td><img src="/images/buttons/vh40.gif"><a href="/html/index.html">HTML Area</a></td>
</tr>
<tr>
<td><img src="/images/buttons/vcss.gif"><a href="/css/index.html">CSS Area</a></td>
</tr>
<tr>
<td><img src="/images/buttons/valid-xhtml10.gif"><a href="/xhtml/index.html">XHTML Area</a></td>
</tr>


Good luck this time Smile
CirTap
Back to top
View user's profile Send private message Visit poster's website
Bamboo
Smarty Rookie


Joined: 06 Aug 2003
Posts: 7

PostPosted: Sun Aug 17, 2003 12:57 pm    Post subject: Reply with quote

wow.... thanks CirTap very much. It work here: [url] www.adt-design.com[/url]

Smile

The first webpage used Smarty. Smile

[/url]
_________________
Thanks for the help.

Best regard,
Bamboo.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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