I’m currently developing a website that i need to have a seperate google map on each page. The google api requires that the code be put in the head of the webpage (template). Each api call is unique, and i don’t want to have 50 different templates. How can I have a seperate head tag unique to a certain page(resource). Before I started using Modx I had an effective way to have a txt file be a database to lookup meta tags based on the page name using php. I know i can create a snippet to call to a database in the same way but I don’t know the modx format to call the database. below is what i used in the past. I know where it says $_SERVER[’DOCUMENT_ROOT’] needs to be changed to something else. and also basename($_SERVER[’SCRIPT_NAME’]);
any help would be great. I want to use a txt file for this particular database and not mysql.
<?php
$database = $_SERVER[’DOCUMENT_ROOT’].’/meta_tags.txt’;
$meta_db = fopen($database, ’r’);
$page = basename($_SERVER[’SCRIPT_NAME’]);
$title = ’’;
$meta_description = ’’;
$meta_keywords = ’’;
while($data = fgetcsv($meta_db, 9000, ’*’))
{
if($data[0] == $page)
{
$title = $data[1];
$meta_description = $data[2];
$meta_keywords = $data[3];
}
}
?>
Haven’t tried this, but you could make one template and associate with it a template variable which is a drop-down list of your various options. Then just change the selected drop-down value in each document you create.
MattC
that will work thank you. But I still would like to know the way to access a txt file database from a php snippet for future reference.
Quote from: landguy at Feb 22, 2010, 09:21 AM
that will work thank you. But I still would like to know the way to access a txt file database from a php snippet for future reference.
I would think just like you did before -- MODx snippets are just PHP code. I assume you were using file_get_contents() and explode() or something similar, which would work fine in a MODx snippet. You might also consider putting the data table in a chunk for easier editing and getting it with $modx->getChunk(’ChunkName’);
FYI, once you have extracted the values you want from the DB/file/Chunk, you can also generate any tags you need in the <head> section and inject them into the <head> section with $modx->regClientStartupHTMLBlock(), something like this:
<?php
/* Meta snippet */
* Injects meta tags into the <head> of the document
*
* Usage: [!Meta? &pageID=`[*id*]` !]
*/
// Set keywords and description variables from table using $id (id of the current document).
$myHtml = '<meta name="keywords" content="' . $yourKeywordsVariable . '" />';
$myHtml .= '<meta name="description" content="' . $yourDescriptionVariable . '" />';
regClientStartupHTMLBlock ($myHtml);
return ''; // return an empty string to make sure there's no output on the screen.
?>
Thanks for the help. you both have good options, i think i’m going to use the template variable. Learning alot Modx is cool
Quote from: landguy at Feb 22, 2010, 05:19 PM
Thanks for the help. You both have good options. I think I’m going to use the template variable. Learning a lot. Modx is cool
I would too. I was just trying to satisfy your curiosity.