Passing variables to a template file without using Smarty

169 views
Skip to first unread message

jsherk

unread,
Aug 21, 2010, 7:58:20 PM8/21/10
to PHPDevShell
Would it be possible to pass variables to a .php template file that
does not use/need Smarty, and then have those variables available
within that php template file? See example below to better understand
what I mean.

REASONING:
Although you can do a lot with Smarty and there is inherently nothing
really wrong with it, I can also do everything Smarty can do in php,
so I do not want to learn how to use Smarty. Maybe some people like
it, but some of us would prefer not to use it. If variables could be
passed directly to another php file (which becomes a php template
file) it eliminates the need for me to learn how to use Smarty.

EXAMPLE:
Script file is example.php and does normal call to template file:
$this->template->set('my_name', $my_name);
$this->template->set('my_age', $my_age);
$this->template->show();

Template file is templates/example.php and would look like this:
<tr>
<td>
Name
</td>
<td>
<?php echo $my_name; ?>
</td>
</tr>
<tr>
<td>
Age
</td>
<td>
<?php echo $my_age; ?>
</td>
</tr>

greg

unread,
Aug 22, 2010, 10:11:41 AM8/22/10
to PHPDevShell
Using Smarty is completely optional. You can even write old-fashion
php code directly into your script. However, using a file such as
"templates/example.php" is your choice and responsability so it's up
to you to decide how to pass the variables. Just remember you are in a
function context, not in the global context.

[ plugins/myPlugin/scripts/myScript ]

$my_name = "greg"
include 'plugins/templates/myTemplate'; // include file will be in the
same context

[ plugins/templates/myTemplate ]

<p class="important">Your name is <?php echo $my_name; ?></p>

jsherk

unread,
Aug 22, 2010, 1:50:14 PM8/22/10
to PHPDevShell
Ok... I now understand what you were saying before...

I can just do this (which does not require/use Smarty):
include('templates/view_example.php');

Instead of this:
$this->template->set('my_name', $my_name);
$this->template->set('my_age', $my_age);
$this->template->show();

And in fact, in the scenario where you have a lot of variables that
would need to be passed using the template->set() function, this
requires a lot less code. So far I like phpds ... it is flexible
enough to allow me to do it how I want!

QUESTION:
Is it possible to retrieve the file name set in the Custom Template
Script Layout field of the Edit Menu Item?

Thanks

jsherk

unread,
Aug 22, 2010, 1:07:39 PM8/22/10
to PHPDevShell
My thinking behind this (as a suggestion or feature request) would be
the ability to still pass the info into your php template file using
$this->template->set('my_age', $my_age);
$this->template->show();
without the need for any include statements.

If the template file is .tpl then process with Smarty... if it's .php
then process it like a php file instead.

This would still allow the separation of controller and viewer without
changing how the code is written, would allow the option to not use
Smarty, and would not 'break' any old code where you wanted to change
from tpl template to php template.

As a side note, one of the features of CodeIgniter is exactly this.

Thanks

greg

unread,
Aug 22, 2010, 2:53:23 PM8/22/10
to PHPDevShell
I'm glad you like PHPDevShell, believe me you'll like v3 even more!

> QUESTION:
> Is it possible to retrieve the file name set in the Custom Template
> Script Layout field of the Edit Menu Item?
Yes it is. Although I'm not familiar enough with v2 to answer that.
Could you please create a question on launchpad so I can have it in my
todo list?

greg

jsherk

unread,
Aug 22, 2010, 3:01:08 PM8/22/10
to PHPDevShell
I will get my feet wet with v2 and I look forward to v3 release!

I will create a question on Launchpad!

Thanks

greg

unread,
Sep 9, 2010, 11:55:03 AM9/9/10
to PHPDevShell
OK I remember now :)

Here's the catch: you can access everything by browsing the system
structures.

For example:

$nav = $this->navigation;
$here = $nav->currentMenuID();
$my_menu = $nav->navigation[$here];
$layout = $my_menu['layout'];

not really handy. I asked you to add a question on LP because I feel
there should be an API to avoid you to mess up with the system data.

greg
Reply all
Reply to author
Forward
0 new messages