xoops forums

fatman

Friend of XOOPS
Posted on: 2004/2/4 16:00
fatman
fatman (Show more)
Friend of XOOPS
Posts: 176
Since: 2003/12/13
#1

smarty and using full path includes

Hi, I'm trying to debug an issue with a module that is in beta. My module uses mod_rewrite to provide clean(er) urls. Because of this every time I include a file I use the full path.

example
// I always use this
include_once(XOOPS_ROOT_PATH.'/modules/include/mycommon.php');

// I don't use this 
include_once('include/mycommon.php');


The problem comes in when I want to pass along the full path to a file which will then be included at the template stage.

probably best to use another example
// In my module code I want to change the template

if ( $cart_items )
{
  
$my_template 'cart_contents.html';
}
 else
{
  
$my_template 'featured_products.html';
}

// Now I assign this to a smarty variable

$xoopsTpl->assign('my_template',
XOOPS_ROOT_PATH."/modules/noah/templates/pages/".$my_template); 

---------------------------------
// Then in my smarty template
---------------------------------

<{include 
file=$my_template}>


This works fine when running on unix/apache and seems to cause problems when running on windows/apache. I think this might be because windows and unix path strings are formatted differently, but I'm not smart enough to confirm this :)

hmmm, is this perhaps an issue of better code organization? As in, should I be making sure that everything happens before..

include(XOOPS_ROOT_PATH.'/header.php');

.. so that I can then dynamically assign

$xoopsOption['template_main'] = 'main.html';

?? anyone have suggestions?

Forum thread with some more background

Mithrandir

XOOPS is my life!
Posted on: 2004/2/4 16:07
Mithrandir
Mithrandir (Show more)
XOOPS is my life!
Posts: 6320
Since: 2003/6/21
#2

Re: smarty and using full path includes

This is a module for XOOPS?

If so, you should use <{include db:FILENAME}> as SMARTY templates are saved in the database in XOOPS.

However, I'd say that re-designing the module pages to set the template_main as early as possible, then the header.php and finally the data retrieval code is a better way to avoid too much logic inside the SMARTY templates.

fatman

Friend of XOOPS
Posted on: 2004/2/4 16:28
fatman
fatman (Show more)
Friend of XOOPS
Posts: 176
Since: 2003/12/13
#3

Re: smarty and using full path includes

yes, the module is for XOOPS and can be demo'd here noah.kerkness.ca

I did not know about <{include db:FILENAME}> and will give that a try. thanks.

fatman

Friend of XOOPS
Posted on: 2004/2/4 17:29
fatman
fatman (Show more)
Friend of XOOPS
Posts: 176
Since: 2003/12/13
#4

Re: smarty and using full path includes

can you provide an example of how to use <{include db:FILENAME}> correctly?

I've tried the following in my module and it is producing fatal smarty class error's on update.

[code]
// in my xoops_version.php
$modversion['templates'][1]['file'] = 'shop.html';
$modversion['templates'][1]['description'] = 'base'; $modversion['templates'][2]['file'] = 'template_name.html';
$modversion['templates'][2]['description'] = 'included template';


// in my module code
$xoopsOption['template_main'] = 'shop.html';
include(XOOPS_ROOT_PATH.'/header.php');
$xoopsTpl->assign('my_template', 'template_name.html');
include(XOOPS_ROOT_PATH.'/footer.php');

// in my smarty template file 'shop.html'
<{include db:$my_template}>

// this produces the following error when
// I am updating the module, so all the template
// details load into the db.

Fatal error: Smarty: [in db:shop.html line 1]: syntax error: invalid attribute name: 'db:$my_template'

[code]

I'm most likely structuring this the wrong way. Any suggestions?

Mithrandir

XOOPS is my life!
Posted on: 2004/2/4 17:35
Mithrandir
Mithrandir (Show more)
XOOPS is my life!
Posts: 6320
Since: 2003/6/21
#5

Re: smarty and using full path includes

<{include file='db:filename.html'}>

I'm not sure whether you can do dynamic includes, but try it out
<{include file='db:$my_template'}>

fatman

Friend of XOOPS
Posted on: 2004/2/4 18:19
fatman
fatman (Show more)
Friend of XOOPS
Posts: 176
Since: 2003/12/13
#6

Re: smarty dynamic included solved

you can do a dynamic include if you use double quotes and not single quotes in your smarty syntax

example

// double quotes work because the when the parser
// sees double quotes it knows to parse the enclosed
// strign for variables.

<{include file="db:$my_template"}>


// When you just use single quotes the parser
// does not bother to look for variables, thus
// saving itself a little effort.
// the following doesn't work
<{include file='db:$my_template'}>



big props to Mithrandir for pointing me in the right direction.

Mithrandir

XOOPS is my life!
Posted on: 2004/2/4 18:27
Mithrandir
Mithrandir (Show more)
XOOPS is my life!
Posts: 6320
Since: 2003/6/21
#7

Re: smarty dynamic included solved

Thread bookmarked
Could be useful to remember this.