Gossamer Forum
Home : Gossamer Threads Inc. : Discussion :

include a filename which is stored in a variable

Quote Reply
include a filename which is stored in a variable
Hi,

I am not sure where to post questions about general GT software modules. Please redirect me if this is the wrong location...

I would like to be able to do something like this
Code:
<%set filename = GenerateFilename('some parameter')%>
<%include $filename%>

If I understand the code correctly, the include files are included during compile time (not during runtime). I would guess that this construction does not allow variable includes. Is that correct? Is there another way to do something like this using the current software or is there a plan to add this feature?

If not, I would like to add this feature myself (if it is at all possible). Can someone give me some hints how to do this?

Thanks, Jasper

http://www.bookings.org
Quote Reply
Re: [jaspercram] include a filename which is stored in a variable In reply to
This is not possible at the moment, but I would also like it if this was added.

You can have a look at the GT/Template/Parser.pm module, around line 440, if you want to add the behaviour yourself.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] include a filename which is stored in a variable In reply to
That is exactly the place where I looked. Smile But my guess was, that if I have to change the system from compile time template inclusion to runtime template inclusion, there might be a lot more to change.....Frown

Jasper

http://www.bookings.org
Quote Reply
Re: [jaspercram] include a filename which is stored in a variable In reply to
I was just trying to come up with a solution for this but it is very tricky due to how the parser works.

At the time when the include file is parsed the value of the variable inside the include tag will not be available. That only becomes available when the template perl code is evaluated.

I tried a dirty solution but that failed too as the random_file function I used is also not compiled until later on and so I ended up with:

Error: Can't load included file: 'e:/apache1/cgi-bin/lsql/admin/templates/default/MyClass::random_file'. Reason: No such file or directory

As you can see it was trying to load a file with the name of the function rather than the returned value.

Unfortunately I see not easy solution to this :(

Last edited by:

Paul: Nov 15, 2002, 4:23 AM
Quote Reply
Re: [jaspercram] include a filename which is stored in a variable In reply to
For the record, I would use the template global generate_tmplname to generate the template name if it exists:
Code:
sub {
my $tags = GT::Template->tags;
my $tmplname = @_[0] || $tags->{do};
my $filename = $tags->{admin_root_path} . "/templates/" . $tags->{template} .
"/" . $tags->{db} . "/$tmplname" . ".html";
return -e $filename ? $filename : "";
}

In my template, I would like to use the subroutine like this:
Code:
<%set actionfile = generate_tmplname('actionlist')%>
<%if actionfile %>
<%include $actionfile%>
<%elsif%>
<%include default.html%>
<%endif%>

This would show a table specific template if it exists and a default template otherwise. Really useful!!!!!!Cool

Jasper

BTW I am not sure how DBMan SQL specific the code above is. Sorry...

http://www.bookings.org
Quote Reply
Re: [jaspercram] include a filename which is stored in a variable In reply to
You could accomplish what you want with:

dynamic_include =>
sub {
my $tpl = shift;
my $tags = GT::Template->tags;
return GT::Template->parse($tpl, $tags, { print => 0 });
}

and then do:

<%dynamic_include($template)%>

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] include a filename which is stored in a variable In reply to
It works! May I kiss you? Wink

Jasper

http://www.bookings.org
Quote Reply
Re: [jaspercram] include a filename which is stored in a variable In reply to
OK, what if I wanted to take data from the record... say the ID, and use that to make an include such as:



<%include <%ID%>.txt%>



So I could include a text bit specific to that ID number?



dave
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] include a filename which is stored in a variable In reply to
I would guess that the easiest way would be, to rename your files to the names of the IDs (without the '.txt' postfix) and use
Code:
<%dynamic_include($ID)%>

Jasper

http://www.bookings.org

Last edited by:

jaspercram: Nov 21, 2002, 1:19 PM
Quote Reply
Re: [jaspercram] include a filename which is stored in a variable In reply to
jaspercram :



Thanks- you the MAN!



dave
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [Alex] include a filename which is stored in a variable In reply to
Quote:
dynamic_include =>
sub {
my $tpl = shift;
my $tags = GT::Template->tags;
return GT::Template->parse($tpl, $tags, { print => 0 });
}


This belongs in Parser.pm, right? Where exactly though?

Thank you
Quote Reply
Re: [Peter544] include a filename which is stored in a variable In reply to
The code Alex posted is a 'template global', which he called 'dynamic_include' and which contains the code
Code:
sub {
my $tpl = shift;
my $tags = GT::Template->tags;
return GT::Template->parse($tpl, $tags, { print => 0 });
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Post deleted by Peter544 In reply to
Quote Reply
Re: [yogi] include a filename which is stored in a variable In reply to
what should the path of the file that is to be inserted be?



Thanks
Quote Reply
Re: [Peter544] include a filename which is stored in a variable In reply to
I believe it is your default template set (/admin/templates/default)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] include a filename which is stored in a variable In reply to
What am I doing wrong?

GLOBAL "dynamic_include":

sub {
# This is so we can include a detail page template
# dynamically based on Detail_Template col in Links table
# usage: <%dynamic_include($Detail_Template)%>

my $veitpl = shift;
my $veitags = GT::Template->tags;
return GT::Template->parse($veitpl, $veitags, { print => 0 } );


CODE FROM the TEMPLATE FILE:

<%if Detail_Template%>
<%dynamic_include($Detail_Template)%>
<%else%>
<%include detailed_std.html%>
<%endif%>

BUILD SAYS:

A fatal error has occured:
GT::Template::Parser (1544): Unable to locate template file 'detailed_std.html' in '.' or any inheritance directories at GT::Template::_compile_template line 627.


Please enable debugging in setup for more details.


Stack Trace======================================Links (1544): Links::environment called at /cgibin/admin/Links.pm line 431 with no arguments.Links (1544): Links::fatal called at GT::Base::error line 362 with arguments (GT::Template::Parser (1544): Unable to locate template file 'detailed_std.html' in '.' or any inheritance directories at GT::Template::_compile_template line 627. ).Links (1544): GT::Base::error called at /cgibin/admin/GT/Template/Parser.pm line 99 with arguments (GT::Template::Parser=HASH(0xa960058), CANTFIND, FATAL, detailed_std.html, .).Links (1544): GT::Template::Parser::parse called at GT::Template::_compile_template line 627 with arguments (GT::Template::Parser=HASH(0xa960058), detailed_std.html, HASH(0xa9235c4), [undef]).Links (1544): GT::Template::_compile_template called at /cgibin/admin/GT/Template.pm line 454 with arguments (GT::Template=HASH(0xa9752fc), detailed_std.html, ./compiled/detailed_std.html.compiled, [undef]).Links (1544): GT::Template::load_template called at /cgibin/admin/GT/Template.pm line 93 with arguments (GT::Template=HASH(0xa9752fc), detailed_std.html, [undef]).It

Build complains that it can't find 'detailed_std.html', yet it is there and the debug show that it compiled it. However, there is no compiled file in the /compiled directory. When I do a normal <%include detailed_std.html%>, it works fine. I'm lost...
Quote Reply
Re: [vicos2] include a filename which is stored in a variable In reply to
Hi,

You might try:

GT::Template->parse($veitpl, $veitags, { print => 0, root => '/path/to/templates/dir' });

and see if that helps.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] include a filename which is stored in a variable In reply to
I needed to include different templates base on the value of some of my tags so I created a custom perl module (IncludeTemplate.pm) and uploaded it to my Dbsql folder. You need to change the blue tag name, file names and template path.
It may help others in the future that need to do the same thing.

IncludeTemplate.pm
Code:
package Dbsql::IncludeTemplate;

use Dbsql qw/$DB $IN $CFG/;
use strict;

sub get_file {

my $tags = GT::Template->tags;
my $file;
my $tpl;

if ($tags->{tagname}){
$tpl = "template1.html";
}

else {
$tpl = "template2.html";
}

return GT::Template->parse($tpl, $tags, { print => 0, root => '/path to the folder the template is stored in' });

#end sub get_file
}
#always end with 1
1;

Add the following in your html template where you want to include the file contents -

<%Dbsql::IncludeTemplate::get_file()%>