Author Topic: "include" filenames from dbase  (Read 7128 times)

0 Members and 1 Guest are viewing this topic.

NickM

  • Guest
"include" filenames from dbase
on: Tue, 24. June 2003 - 06:04:01
I have a number of "include" files as plain HTML that I would like incorporated into my template file.
The docs say, and this works:
<tmpl_include name="./includetxt/principles.html">

What if I want to pull the name of that file off the dbase?
I've tried this but can not make it work:
<tmpl_include name="./includetxt/<tmpl_var name="inc_name">">

Where "inc_name" is the name of the HTML file in the database (e.g. principles.html - as in the first example above).

Any suggestions?

releasedj

  • Guest
"include" filenames from dbase
Reply #1 on: Tue, 24. June 2003 - 11:17:39
Sorry, that isn't possible. It's functionality which would be nice to have, but hasn't been added as of yet.

NickM

  • Guest
"include" filenames from dbase
Reply #2 on: Tue, 24. June 2003 - 11:41:05
OK so what's a work around - Maybe reading the file in PHP and allocating it to a VAR then just writing <tmpl_var name="the file that has just been read"> and it will show up?
OR something else?

releasedj

  • Guest
"include" filenames from dbase
Reply #3 on: Tue, 24. June 2003 - 11:48:28
Yeah, I would say that its your best bet. However, if that file contains <tmpl_*> tags, they wont be translated.

Also, when you place the <tmpl_var tag, remember to turn escaping off for that tag, i.e.:

Code: [Select]
tmpl_var name=myfile escape=none
Last Edit: Wed, 16. February 2005 - 01:09:56 by ClausVB

NickM

  • Guest
"include" filenames from dbase
Reply #4 on: Tue, 24. June 2003 - 15:20:58
I take it that I won't be able to use that wonderful setDbLoop?
I love that option, but is seems a little limited. As soon as you want to go beyond just pulling the data, you have to go into your Loop options - yes?

releasedj

  • Guest
"include" filenames from dbase
Reply #5 on: Tue, 24. June 2003 - 15:50:23
How do you mean? Use it how?

NickM

  • Guest
"include" filenames from dbase
Reply #6 on: Tue, 24. June 2003 - 16:21:30
Code: [Select]
?php
include_once../CTA_dbase/vlib4/vlibTemplate.php;
tmpl = new vlibTemplate../CTA_dbase/templates/cta_template.htm;
tmpl-setVartitle, CTA for all your coolstorage needs;
tmpl-newLoopmyLoop;

require_oncedb_fns.php;
conn = db_connect; //function to connect
sql = select * from cta_pg where nav_name=nav_name;
result=mysql_querysql, conn;

While row=mysql_fetch_array result {

      filename=rowinc_name;                //name of the file
      filename=./includetxt/.filename;     //dir to the file

      fp= fopenfilename, r;            //read the file
While feoffp {
      fileshow= fgetsfp,10000;
tmpl-setVar this_file,fileshow;          //set template name
       }//close second while

tmpl-addLoop;                                 //add template vars
}//close first while
tmpl-pparse;
?
----------------------------
and the cut-down template:
----------------------------

Code: [Select]
tmpl_loop name=myLoop

img src=images/tmpl_var name=photo_name

div class=heads align=centertmpl_var name=head/div

p class=subheadtmpl_var name=section/p

tmpl_var name = this_file escape=none
Im not getting any errors - but Im not getting anything showing up in my template either.
There is a trick in the PHP/VLib that Im just not grasping.
I hope you can make sense of what Im trying to do -
simply show what is in the dbase, except for the HTML file (only the name  is in the dbase) where I have had to read it and assign it to its own tmpl_var.
Can you help?
Last Edit: Fri, 24. March 2006 - 08:23:37 by ClausVB

NickM

  • Guest
"include" filenames from dbase
Reply #7 on: Tue, 24. June 2003 - 16:31:23
Regarding the use/restriction of DBLoop - this is what I had initially and it worked wonderfully.
As soon as I had to do something that required pulling database info and manipulating it - as per above example - I had to go with newLoop etc.
Unless there is an easier way...

include_once('../CTA_dbase/vlib4/vlibTemplate.php');
$tmpl = new vlibTemplate('../CTA_dbase/templates/cta_template.htm');
$tmpl->setVar('title', 'CTA for all your coolstorage needs');
require_once("db_fns.php");
$conn = db_connect(); //function to connect
$sql = "select * from cta_pg where nav_name='$nav_name'";
$result=mysql_query($sql, $conn);
$tmpl->setDbLoop('myLoop', $result);
$tmpl->pparse();

releasedj

  • Guest
"include" filenames from dbase
Reply #8 on: Tue, 24. June 2003 - 17:13:43
I'm going to look into a way of parsing variables like this.

It may take a while though, as I'm a bust person.

NickM

  • Guest
"include" filenames from dbase
Reply #9 on: Wed, 25. June 2003 - 00:53:28
Ok, so there is no point persevering with this because it just won't happen (I could have been sleeping instead of coding!)
There is no way to parse via vLib this sought of thing?
My best bet is to have the actual file, not just its name in the dbase - then I can treat it like any other bit of info - except for escape=none.
Any other suggestion for handling bits of HTML that just need to be inserted into a template?

NickM

  • Guest
"include" filenames from dbase
Reply #10 on: Wed, 25. June 2003 - 01:34:43
I just heard that Smarty can do this - anyone out there know if that's true?

NickM

  • Guest
"include" filenames from dbase
Reply #11 on: Wed, 25. June 2003 - 03:53:15
Hold on there...
Just had a look at Smarty.  :roll:
I think I'll wait for you to add this functionality - any idea of when?

NickM

  • Guest
"include" filenames from dbase
Reply #12 on: Wed, 25. June 2003 - 15:46:15
Kelvin,
Is there no way (any workaround) to just pop a selected file into a template?

releasedj

  • Guest
"include" filenames from dbase
Reply #13 on: Wed, 25. June 2003 - 16:34:42
TBH I think it would be a bit of a hack to add functionality for accessing data in a database.

However I do think I could add functionality whereby you can have, for example:

Code: [Select]
tmpl_include file=templates/include/{vartop_nav}.html
Where top_nav would be a variable like the ones used in <tmpl_var> tags.

I just have to find the time to do it. I should be able to do it in the next couple of days, and then Ill release a new candidature.

Kelvin
Last Edit: Wed, 16. February 2005 - 01:10:22 by ClausVB

NickM

  • Guest
"include" filenames from dbase
Reply #14 on: Thu, 26. June 2003 - 02:39:04
Thanks Kelvin, I look forward to the added functionality.
It's going to make life easier for me.

On the PHP side of this issue though:
Why can't we read a file into a variable say $read_file (the file name is selected from the dbase and read using fopen and a while loop) and then just do this:
tmpl->setVar ('show_file', '$read_file');

And in the template:
<tmpl_var name ="show_file">

In my simplistic understanding (of PHP and vLib), that should print the variable, which happens to be the file we want (e.g. a txt or HTML file, where the name was selected from the dbase).

There is something wrong with this logic?

releasedj

  • Guest
"include" filenames from dbase
Reply #15 on: Thu, 26. June 2003 - 11:34:28
No. It's quite simple to put that data into a variable.

The problem comes when you want that data to be parsed for <tmpl> tags.

NickM

  • Guest
"include" filenames from dbase
Reply #16 on: Mon, 30. June 2003 - 14:50:42
Kelvin said: "I just have to find the time to do it. I should be able to do it in the next couple of days, and then I'll release a new candidature."

Just thought I'd check. Have you had any time? :wink:

releasedj

  • Guest
"include" filenames from dbase
Reply #17 on: Sat, 05. July 2003 - 02:10:40
I did start to look into it last week, alas, this feature will take a little more time and at the moment I'm on holiday.

I will check back in a couple of weeks with an update.

NickM

  • Guest
"include" filenames from dbase
Reply #18 on: Mon, 07. July 2003 - 01:24:33
Enjoy your break.
I was just wondering...
Is it possible to parse the file name only, and have the file included some other way?
I read somewhere something about an SSI, or a bit of Javascript using an "include" function in the HTML page e.g.
<!--#include file="yourtextfile.txt" -->
Any thoughts on using these alternatives to achieve the same thing?
Would you have any other suggestions?

NickM

  • Guest
"include" filenames from dbase
Reply #19 on: Fri, 18. July 2003 - 15:11:11
:wink: Just checking in to see if there has been any progress on this one.

releasedj

  • Guest
"include" filenames from dbase
Reply #20 on: Fri, 18. July 2003 - 18:07:43
Working on it and getting close to the solution!

Will let you know when I have someting for you to play with.


Regards,

Kelvin

releasedj

  • Guest
"include" filenames from dbase
Reply #21 on: Wed, 23. July 2003 - 14:00:54
Ive made changes to the vlibTemplate.php file in CVS.

This allows you to specify a variable name within your template file that is looked up to get the final name of your template to include, i.e.:

Code: [Select]
main.php
?php
require_once vLIB/vlibTemplate.php;

t = new vlibTemplate main.html;
footer = isset_GETfooter ? _GETfooter 1;
t-setvarfooter, footer;
t-pparse;

?

main.html

html
body

Here is the page content.

br /br /

Heres the page footer.br /

tmpl_include file=footer_{varfooter}.html /

/body
/html

So if the footer var is set to 1, then it will include footer_1.html.

You can get the latest code from viewcvs on sourceforge. This takes about 24 hours to update so the file might not be there yet:
http://cvs.sourceforge.net/cgi-bin/viewcvs....viewcvs-markup

Have a mess with it and let me know how it goes.

Regards,

Kelvin
Last Edit: Wed, 19. October 2005 - 22:39:37 by ClausVB

NickM

  • Guest
"include" filenames from dbase
Reply #22 on: Thu, 24. July 2003 - 08:13:53
Many Many Thanks Kelvin.
Sourceforge has not updated yet so I have not had a chance to play.
But I do have some questions:

In the PHP - 'footer' is the field name in the dbase?

I dont't understand the code so, does the number 1 here:
...? $_GET['footer'] : '1';
have anything to do with the your comment that
"if the footer var is set to '1'

In the template - can you have this:
<tmpl_include file='{var:footer}.html"

Now for a practical example:
If I had a dbase with a filed name "middle_text"
and two entries                             text1
                                                  text2

Then this would be the code
main.php:

<?php
require_once ('vLIB/vlibTemplate.php');
$t = new vlibTemplate ('main.html');

$middle_text = (isset($_GET['middle_text'])) ? $_GET['middle_text'] : '1';
$t->setvar('middle_text', $middle_text);
$t->pparse();
?>

main.html

<html>
<body>

Here is the page content.
<br /><br />
Here's the middle text
<br />

<tmpl_include file="{var:middle_text}.html" />
</body>
</html>

And this part of the code:
<tmpl_include file="{var:middle_text}.html" />
would include text1.html OR text2.html depending on which one was called from the dbase.

Sorry for being so long-winded; but I'm excited with the possibilities.

releasedj

  • Guest
"include" filenames from dbase
Reply #23 on: Thu, 24. July 2003 - 10:11:30
When I was testing I was just using 2 files to include called footer_1.html and footer_2.html. I would then pass through main.php?footer=2 in the query string to change the footer content, this is the reason for the followinf line:

Code: [Select]

$footer = isset$_GET'footer' ? $_GET'footer' '1';



Your example would also work. It will very simply parse the file="" parameter of the tmpl_include tag and for {var:???} and replace it with the value that has been assigned.

This still needs a little testing however, but it should work, even with vlibTemplateCache.

Hopefully the sourceforge viewcvs will be updated today.

Regards,

Kelvin

NickM

  • Guest
"include" filenames from dbase
Reply #24 on: Thu, 24. July 2003 - 14:43:06
Starting to understand.
According to your last post:

$footer = (isset($_GET['footer'])) ? $_GET['footer'] : '1';

You would change '1' to '2' for testing.

So if I don't have '1' or '2' to worry about then does this work
(code as above but without reference to the '1')

$footer = (isset($_GET['footer']));

NickM

  • Guest
"include" filenames from dbase
Reply #25 on: Thu, 24. July 2003 - 14:45:32
Or maybe I don't need that line at all?

releasedj

  • Guest
"include" filenames from dbase
Reply #26 on: Thu, 24. July 2003 - 15:26:49
You don't need that line, it was just the way I used to switch between 2 template files.

You just need to choose the template you wish to include by whatever means necessary (i.e. as db call, a switch statement ...etc.), and then assign a variable to be used by the template.

keo

  • Guest
"include" filenames from dbase
Reply #27 on: Sat, 26. July 2003 - 04:05:45
Perfect timing!

I just started working whith vLIB today and had the same problem.. Actually my source was older, so it didn't work. I tested some ideas mentioned above but they didn't work..

This thread saved my day ;-)
Thanks alot for your work!

NickM

  • Guest
"include" filenames from dbase
Reply #28 on: Sat, 26. July 2003 - 08:37:46
Kelvin,
If I have a field name in the dbase called "nav_name"
and I use setDbLoop to retrieve records from that field,
will this work in the template:
<tmpl_include file="./subdirectory/{var:nav_name}.html">

I ask because I can get it to work when I include a specific file name, e.g.
<tmpl_include file="./subdirectory/contact.html">

And I can get it to work when in the PHP:
  $tmpl->setVar  ('text',$nav_name);
(in this case $nav_name comes from the URL from another page)
And in the template, using the {var:text}.html

but it does not seem to work when nav_name is a record drawn directly from the dbase.

Am I missing something?

releasedj

  • Guest
"include" filenames from dbase
Reply #29 on: Sat, 26. July 2003 - 23:19:11
The reason it doesn't work is because it doesn't work within loops.

Loops have a completely seperate structure to global vars, so , to keep it simple, I left it so that you can only access global vars using the {var:} syntax.

If you get the variable from the database manually and then set the vat using setvar() this will work as expected.

NickM

  • Guest
"include" filenames from dbase
Reply #30 on: Sun, 27. July 2003 - 06:14:32
That makes sense.
I have already implemented the added functionality to a website for a client (my first using PHP [and vLib]). Vlib has made the job that much easier.
Many thanks for the program, the quick responses and the help.
Now onto other vLib issues...

dieterweb

  • Guest
"include" filenames from dbase
Reply #31 on: Sun, 02. November 2003 - 11:36:47
i cant get it to work with {var:footer}.

(index.php)
$tmpl->setVar('footer', 'news');

(template.html)
<tmpl_include file="{var:footer}.html" />

but it doesnt work. i can use the var with {tmpl_var name='footer'} but not in that way. what am i doing wrong? when i use <tmpl_include file="news.html" /> it works.

releasedj

  • Guest
"include" filenames from dbase
Reply #32 on: Thu, 06. November 2003 - 17:09:25
Which version of vLIB are you using?

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
"include" filenames from dbase
Reply #33 on: Fri, 11. November 2005 - 20:07:17
Works perfectly

Code: [Select]
tmpl_include file={varheader}
tmpl_include file={varbody}
See
/docs/multihtml/vlib...pl_include.html
for further details.

Regards,
Claus

@German users: Mehr kann man unter Modulares Programmieren mit TMPL_INCLUDE nachlesen.
Last Edit: Thu, 24. November 2005 - 12:44:41 by ClausVB