3

I got problem when implementing my CMS using Codeigniter 1.7.2 and Dwoo. I use Phil Sturgeon Dwoo library. My problem is I want user create template from the admin panel, it means all template will be stored into database including all Dwoo variable and functions.My questions:

  1. Is it possible to load dwoo template from database?
  2. How to parse dwoo variable or function from database? I tried to load content from database which is include dwoo var and function inside it, and i have tried to do evaluation using dwoo eval() function and phil sturgeon string_parse() but still have no luck.

for example:

my controller

$data['header'] = "<h1>{$header}</h1>"; --> this could be loaded from database

$this->parser->parse('header',$data);

my view

{$header}

This is the error message:

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  header_title</p>
<p>Filename: compiled/805659ab5e619e094cac7deb9c8cbfb5.d17.php</p>
<p>Line Number: 11</p>

header_title is dwoo variable that loaded from DB.

Thank you,

fancyPants
  • 50,732
  • 33
  • 89
  • 96
pylady
  • 129
  • 1
  • 2
  • 9

1 Answers1

1

It is definitely possible to do this, but for performance reasons it would probably be faster to store the templates as files on the filesystem, even if they're edited by users. If you're smart with file naming you can avoid most hits to the database.

Anyway, if you really want to do it with the database, the following should work:

// rendering the header
$template = '<h1>{$header}</h1>'; // loaded from db, don't use double-quotes for examples or $header will be replaced by nothing
$data = array('header' => 'Hello'); // loaded from db as well I assume
$headerOutput = $this->parser->parse_string($template, $data, true); // true makes it return the output

// now rendering the full page (if needed?)
$data = array('header' => $headerOutput);
$this->parser->parse('header', $data); // no 'true', so goes straight to output

The view would then contain {$header} and the output from the header template is passed to that variable.

Now I'm not sure how CI works so it might be better to just output the result of the first template and skip the second one entirely, but I'll leave that up to you.

Seldaek
  • 40,986
  • 9
  • 97
  • 77
  • Thank you seldaek, I will try it now and let you know the result. – pylady Dec 26 '10 at 12:45
  • Hi Seldaek,It doesn't work. I dont know maybe the problem is from phil's library, I'll try to check the library first. – pylady Dec 26 '10 at 14:42
  • Got it!!!I forgot my dwoo variable is $header_title not $header. My mistake, thankx seldaek – pylady Dec 26 '10 at 15:35
  • Thanks for covering for me there Seldaek, Google Alerts only just sent me an email about this one. >. – Phil Sturgeon Dec 30 '10 at 14:21
  • @RedTruck: Glad you got it to work. If you have a minute to accept my answer as the solution to your question, it'd be nice :) – Seldaek Dec 30 '10 at 20:02