How to Output PHP Code through CMSB?

12 posts by 4 authors in: Forums > CMS Builder
Last Post: February 14, 2011   (RSS)

Re: [Perchpole] How to Output PHP Code through CMSB?

By Toledoh - February 7, 2011

Hi Perch.

I've got a drop down in the admin that selects the style. This style is then applied to a page via CSS.

ie.

<link href="css/<?php echo $record['caption'] ?>.css" rel="stylesheet" type="text/css">

I've also been wanting to implement this http://css-tricks.com/css-variables-with-php but haven't gotten to it as yet.
Cheers,

Tim (toledoh.com.au)

Re: [Perchpole] How to Output PHP Code through CMSB?

By Jason - February 8, 2011

Hi Perch,

Using php code as a list value isn't going to be a good solution as it could give you some unexpected results. Also, it's harder to guarantee uniqueness.

Here's a different approach you could try.

Have a multi-record section called styles. Here all you need to store is a title (for use as a label) and a path to an include file. (just a text field). You can now use this as the table for your drop down, using the sytles section's num field as your option value.

Now when you've selected a record, you can ouput your selected style like this:

<?php if($record['style']): ?>

<?php
//get the selected style record
$query = "SELECT * FROM `{$TABLE_PREFIX}styles` WHERE num = '".intval($record['num'])."'";
$styleRecord = mysql_query_fetch_row_assoc($query);

//include style path

include($styleRecord['path']);
?>

<?php endif ?>


This example code assumes that your drop down field is called style, your multi-record style section is called styles, and your path text field is called path.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Toledoh] How to Output PHP Code through CMSB?

Hi Tim,

Using CMSB to populate a CSS stylesheet is pretty straight forward.

Here's a recipe from my CMSB Cookbook http://www.thecmsbcookbook.com that describes the approach.

Hope it makes things simpler.

Jerry Kornbluth

USING CMSB TO POPULATE A CSS STYLESHEET

It’s easy to populate a CSS stylesheet. from fields in a CMSB editor.

Here’s how:

In this example we'll modify the following CSS stylesheet to pull data from text fields called "heading_size", and "heading_color", and an image upload field with one uploaded image called "page_background_image", in a single record CMSB editor called "common_information".

You can apply this technique to any CSS element.

#header {background-image:url(../images/your_image.jpg)};
}

h1{
color: #c0c0c0;
font-size = 2em;
text-align: center;
}


If you've ever modified a site to treat html pages as PHP, using an .htaccess file, you might think that inserting:

Addhandler application/x-httpd-php.css

in the .htaccess file would be an adequate approach to recognizing .css files as PHP, but in many situations,this can lead to unexpected errors in how your pages are rendered.

So, to be safe, I'd use this approach.

First, add a .php the extension of your .css file (.css.php)

Then, in the head of your viewer page you’ll need to change:

<link rel="stylesheet" type="text/css" href="your_css_folder/your_css_file.css" />

to:

<link rel="stylesheet" type="text/css" href="your_css_folder/your_css_file.php" />

Now add the following PHP call before any CSS code to insure that your .css.php document is rendered correctly.

<?php

header("Content-type: text/css");
?>


After you’ve created the necessary fields in your editor and entered some sample values, go to the code generator for your editor and copy the PHP require once and get records call to the head of your css file.

NOTE: DO NOT INCLUDE THIS LINE:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "path_to_your/cmsAdmin/lib/viewer_functions.php";

list($common_informationRecords, $common_informationMetaData) = getRecords(array(
'tableName' => 'common_information',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$common_informationRecord = @$common_informationRecords[0]; // get first record

// show error message if no matching record is found
if (!$common_informationRecord) {
print "Record not found!";
exit;
}

?>
<?php
header("Content-type: text/css");
?>

#header {background-image:url(<?php foreach ($common_informationRecord['page_background_image'] as $upload): ?>
http://www.your_site.com/<?php echo $upload['thumbUrlPath'] ?><?php endforeach ?>)};
}

h1{
color: #<?php echo $common_informationRecord['heading_color'] ?>;
font-size = <?php echo $common_informationRecord['heading_size'] ?>;
text-align: center;
}


That’s it, you can replace any css element with a php call and give your clients added flexibility without them needing to re-code your CSS stylesheet.
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] How to Output PHP Code through CMSB?

By Toledoh - February 9, 2011

Thanks Jerry - great to know it can be done.
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] How to Output PHP Code through CMSB?

Hello, All -

Thanks for your feedback and interesting ideas! Sorry for not responding sooner but I've been on holiday...

Whilst I am very grateful for your suggestions, my particular issue does not concern style sheets.

As I said in my original post, I want to be able to dynamically select the page template whilst creating a record in the CMSB backend.

As things stand I have a base template which includes all of the header and footer info. The user chooses which layout to use for the page content at the time of making the record. They do this by selecting a layout from a list. Each template corresponds to a number. Then it's just a case of hardwiring the code into the base template, something like this...
<?php if ($record['page_type']==1): ?>
<?php include ("_template_1.php") ?>
<?php elseif ($record['page_type']==2): ?>
<?php include ("_template_2.php") ?>
etc...



When the base template loads the layout is also loaded as per the users selection.

It works fine. The only trouble is you have to keep editing and updating the if/elseif arguments in the base template. This can get long and messy.

What I'd like to do is be able to insert the code for the chosen template directly via the selection made in CMSB. So if the user chooses template 1 for example the code for...
<?php echo $record['page_type'] ?>
is replaced with...
<?php include ("_template_1.php") ?>

The key (as far as my limited knowledge is concerned) is making CMSB output code, in this case the php include. As far as I'm aware this cannot be done (unless I've missed something) which leads me to think I'm approaching the problem from the wrong direction...

:o/

Perch

Re: [Perchpole] How to Output PHP Code through CMSB?

Hi Perch.

I'm very interested in this - so please don't think I'm arguing :)

Can you give some examples of the templates?

My way of thinking is that if the data is the same, you really should be able to control the whole look and feel of the site via the templates.

I'm looking at a site currently where each member will be able to choose a "template" in which to display their profile. I want those profile templates to be dramatically different - not just one with blue text, the other with red.

For instance, look at
http://www.csszengarden.com/
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] How to Output PHP Code through CMSB?

Hi, Tim -

My approach is possibly a little different. My Holy Grail - so to speak - is to present a site to the viewer through a single CMSB powered page (index.php). Essentially, I want everything to be clean and lean!

This would be easy if all pages on a site were the same. The trouble is they're not.

The content on the home page is very different to the details you'd find on a contact page, for example. You may also want to put special items of interest or other gizmos in the margins of a page or side-bar.

To do all of these things you need to be able to control the page layout on a per-page basis. That way you not only get to choose the content that appears on the page but also how (and where) it is presented.

With this in mind, I am trying to perfect a simple method where-by the user can select the layout at the time of creating a record in CMSB.

Imagine the scenario: The user creates a new record in CMSB. It's a news story. He needs to show certain details in the side-bar which relate to other news stories on the site. There's also an ad' for a local sponsor.

To do all of this he selects a "News" template from a drop-down list. This has all of the required elements piped in already. The page is rendered to the viewer accordingly. It looks like a news page.

If - for the sake of argument - he had chosen the "Contact" template, which has a completely different layout, the info from the CMSB record would still feed in but none of the other elements would be visible. It would look like a contact page.

It is this capacity to change the layout/template on the fly which I find so appealing and which is why I'd like to nail a clean solution.

:0)

Perch

Re: [Perchpole] How to Output PHP Code through CMSB?

So where I'm saying - "I want a news page, that lays the news out in 3 columns, or a head article followed by 2 columns - you're saying you would like a page, with a news article, plus a gallery feature and a feedback form?

I think I understand - and it's a good question.

Gotta be a way... let's keep nutting it out!
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] How to Output PHP Code through CMSB?

Exactly. You choose the template/layout to suit the content.

First you need to set-up all of your templates, including static content (like ad's) plus data from other sections of the site (like news feeds).

Then, when you create a record in CMSB, you can choose how it looks.

Maybe you want a full-width page or the three-column variant.

Perhaps you want the one with the margin and the search box...

...or you need the one with the special footer...

Etc...

Maybe you're creating a mini gallery. You don't need any text to appear on the page. You just want all of the Uploads to appear in a grid-like layout.

The point is you get to choose that layout when you create the record in CMSB.

:0)

Perch