views:

48

answers:

2

Hi There.

I am working on an existing website that uses Zend Framework, and I am trying to avoid hard-coding content that needs to be dynamic in nature.

I have a list of articles that need to be output as links with or without a description. Each link generates a dynamic page that will output the full article with a breadcrumb and a link list of the other article titles, saving state to highlight the current article. I have a template ready to take each variable, but first I need a content source.

Because the list is somewhat short, we do not feel it should be stored in the database (how would I even enter/edit said content in the database without a CMS?).

Can this be done by parsing an XML document, or is there another method I am not thinking of? I've used SimpleXML on non-Zend PHP sites but calling the class directly doesn't seem to work (I think it might be a path issue).

EDIT: I ended up using the solution recommended by prodigitalson:

I created an XML file for my content's meta data (title, hash, description, include file) and put it in my app/modules/default/model folder. I then laid out each full article as an include snippet, and put the files in views/partials. I created a custom class with the methods I needed and put it in my library folder -- then, I instantiated my class in the controller's init() method. Then, the object was available to all pages/actions within that controller.

Then, I can call any method from the controller-instantiated object in a corresponding view. I'm able to output links, full articles and everything else I need by using this approach. I'm passing the current URL param (hash) to the class before outputting the link list in order to highlight the current link.

+2  A: 

Id probably stick with XML (or ini if you prefer) to store the meta info for the articles (path to html file, nav title, url/url params).Then instead of SimpleXML id use Zend_Config_Xml (or ini). to pull int he info. From there it shoudl jsut be a matter of pulling the varibales you need out of it and including the actual content file (which i would keep completely separate from the config - just store the path to the "content" file).

prodigitalson
Thanks, this sounds good. When you include the content file (with the filename supplied from XML), do you use a regular PHP include or some Zend function?
Marcy Sutton
Well thats kind of up to you and depends on your requirements. I would think at the very least you'll want to buffer the content into a string variable for `echo` in your template so the best thing to do would probably be to make a view helper that does this. But the partial helper already implements this, so i would just extend that to read from your "content" directory or change the config of the app to also read partials from this content dir so you can load the content like typical partials. But you should do whatever you think will work best in your app.
prodigitalson
OF course if you dont want to parse the content as PHP then you would need to make your helper use `file_get_contents` instead of include.
prodigitalson
A: 

Use an array. You can put your data in a separate file :

// in data.php
return array(
  array("data1", "data2"),
  array("data3", "data4")
);

// to access data
$data = require('data.php');

It's simpler and faster (and ugly but it's PHP ;-) )

Maxence
Personally I hate this approach. I don't care if its faster... I just hate the idea of a config file being in the native scripting language as opposed to config/data interchange format (excluding JSON). Its still definitely a valid answer - I just cant bring myself to do it :-)
prodigitalson
As we say in french : "Peu importe le flacon pourvu qu'on ait l'ivresse" (regardless of the bottle long as you have drunk)
Maxence