Understanding Single Pages (A primer)

Permalink
Most of us know that blocks provide an extensible interface to C5 by allowing us to reuse certain types of content across multiple pages with ease. What is less known is that there is another way to extend C5 sitting right under our noses called "Single Pages".

Single Pages fulfill a need for functional content that will not be used in other areas of the site. A good example of a single page that ships with C5 is the login page. Single pages exist in the single_pages folder and are backed by a controller of the same name in the controllers folder. So a single page called widgets would be setup as so:

$C5_ROOT/single_pages/widgets.php
$C5_ROOT/controllers/widgets.php


The template "view.php" from your theme will be applied to your single page. In order to print the content of the single page, the content area of view.php should look like:

<?php print $innerContent; ?>


The controller of a single page is much simpler than a block in that it doesn't have to define all of the options for the single page since it won't be reused. A basic controller for our widgets single page would look like:

<?php
class WidgetsController extends Controller {
   // any number of public methods can go in the controller.
   public function hello_world() {
   }
}


Addressing controller methods from within your single page "view" is slightly different than a block. It would appear that single pages run within an instance of an object, and there is an instance variable that contains a controller object. So if you have a hello_world() function in your controller, you'd access it from your single page view as such:

<?php 
$this->controller->hello_world(); 
?>


You can also automatically submit to single pages like this, from within your view.

<form method="post" action="<?=$this->action('hello_world')?>">
</form>


Putting the pieces together, let's make our widget single page fly! To install the single page enter the C5 admin and visit the page types interface. At the bottom of the page, there's an option to install a new single page. Type widgets in the field and click the button. Widgets will be added to the sitemap. While single pages can have children, they cannot be children. They will always exist on the 2nd level of the site map (one level below home).

Now visit your page:http://YOUR_DOMAIN_HERE/widgets...

There's much more you can do with Single Pages, this is just a primer to get you up and running! Jereme

jereme
View Replies:
jereme replied on at Permalink Reply
jereme
It appears my little primer is missing most of its PHP examples. I'm awaiting a fix from the cats at C5. j
andrew replied on at Permalink Reply
andrew
Sadly I think we did some dumb things with strip_tags and the like. I think we're ditching markdown in favor of a simpler approach (like the sourceforge forums) but with a way to deal with code blocks specifically.
jereme replied on at Permalink Reply
jereme
Any suggestions for resurrecting the php in my post?
andrew replied on at Permalink Reply
andrew
Rather than running everything through htmlentities() we were running strip_tags, which is obviously not a very smart thing to do on a site where a large number of posts will deal with HTML and PHP topics.
andrew replied on at Permalink Reply
andrew
Jereme,

I wasn't able to retrieve everything you had in here (sorry about that!) but I edited the text above and inserted some pseudo-code that hopefully makes this more helpful.

And to everyone reading this - more documentation on using single pages (and the MVC-style syntax that C5 allows with pages/page types) is coming very soon to the documentation section.
jereme replied on at Permalink Reply
jereme
Wow Andrew thanks! I was going to repost, so I _really_ appreciate you taking that on.
alpeb replied on at Permalink Reply
Thanks for mini-tutorial jereme.

I've created the file /themes/mytheme/view.php, and it works good for newly created single-pages. However I can't figure out how to apply this to concrete's login page. Any pointers?

Thanks,
Alejandro
andrew replied on at Permalink Reply
andrew
Certain core pages of C5 have their themes specified by path. This includes /login/. However you can override this by setting up your own theme paths.

http://www.concrete5.org/help/documentation/developers/themes/speci...
andrew replied on at Permalink Reply
andrew
we really gotta do something about making long links wrap ;)
frz replied on at Permalink Reply
frz
seems like we are cause mine's truncated in display above but the url is wrong. perhaps a div's edge just needs to be brought in if it's a reply?
alpeb replied on at Permalink Reply
Thanks. The only thing I'm missing now is the ability to add blocks to these single pages, not possible yet, correct?
DavidMIRV replied on at Permalink Reply
DavidMIRV
Is it possible to access/use a custom controller (Want my own Registration Controller) For the internal site pages? I created a dummy controller in the top level controllers/ directory but accessing it in the same way that say the built in registration form accesses it's controller doesn't work.. e.g. index.php/register/-/do_register ...
jereme replied on at Permalink Reply
jereme
Alejandro,

You can add blocks to a single page just like you can a page type. Single pages have an edit mode one in the same.
jpabellon replied on at Permalink Reply
jpabellon
You said:
"Widgets will be added to the sitemap. While single pages can have children, they cannot be children. They will always exist on the 2nd level of the site map (one level below home)."


From the sitemap I was able to make it a child page. Seems to work fine. Is this a new update?
matogertel replied on at Permalink Reply
matogertel
Is it just me or does anyone else finds regular pages more flexible than single pages?
I mean, I can do exactly the same things with one and the other, even have custom controllers and that and have my code consistent across all pages, and keep all pages inside my theme instead of all over the place!
I don't have to be thinking about whether a page is a single page, and then the controller name is slightly different, and I need to add it somewhere else, and the page itself seats outside my theme folder, which makes the use of the "inc" function inconsistent, cause it's looking for elements in the theme... etc...
Anyone else had the same experience?