Jump to content

Site Title?


Lance O.
 Share

Recommended Posts

I've seen references to the page title, but where in ProcessWire can I set the site title, and how do I include it in my templates?

My total experience with ProcessWire is about two hours, but it looks very promising. I develop primarily in WordPress, but the "post" structure has proved itself to be limiting.

Link to comment
Share on other sites

Hi lance, welcome.

PW is very flexible and has no "Site" title as it's mostly something you set/code in your template. However you could use any field or page title field as the site name from any page you like. So maybe use the root node "home" on the top of the tree to define a site title for your website.

To for example simply output the title from the root page use

echo $pages->get("/")->title;

Or if you want you could create a simple textfield ie. "site_title" and add that to the "home" template in PW. Then you simply change the above to

echo $pages->get("/")->site_title;

Another possible route is to use config file to add a title

in /site/config.php

$config->site_title = "MyHomepage";

then to you can access it from any php template like

echo $config->site_title;
  • Like 4
Link to comment
Share on other sites

There ain't such a thing by default. You can do template "settings", with fields like "Site title", "Copyright text" etc... and create one page with that template which keeps your "site settings" if you need to. But usually it is just better to hard code stuff like that to your template file. Site title is thing that rarely changes and if you have build your templates well, you need to change it from one file only.

So in default site profile you would edit head.inc file and add your site title to <title><?php echo $page->title;?> - Site title</title>

And welcome to the forums Lance!

EDIT: This forum sometimes let you know about other replies before you, sometimes it just posts it :/

Link to comment
Share on other sites

Hi Lance. I actually don’t think there is one as you’d expect it from CMS you may be used to.

I usually use the home page’s title as the site’s name and create a var $home at the top of the header include like this:

<?php
if($page->path == "/") {
 $home = $page;
} else {
 $home = $pages->get("/");
}
?>

and use it e.g. in <title></title> like this:

<title><?php echo $home->title.($home->id != $page->id)?" – ".$page->title:""; ?></title>

Sometimes I create a custom text inputfield for the site’s name and add it to the home template, so I can take it from there like this:

$pages->get("/")->site_name;
// or if used like above
$home->site_name;

When I use the $home solution, I often use the home template to store general information on the site. But you also could create a hidden page as an info container for this.

Link to comment
Share on other sites

Lance I'm not sure specifically what a site title is, but guessing you just mean some common string of text that's repeated on every page? If so, I would probably add a text field to your homepage template and call it 'site_title' or whatever you want. Then have your main template (or head.inc) output that text wherever you want it to appear. For instance:

<?php
$site_title = $pages->get('/')->site_title;
echo "<a id='logo' href='{$config->urls->root}'>$site_title</a>";

---

Edit: holy smokes, there were no replies when I started writing. I hit reply and now there's 3. :) You guys are fast.

Link to comment
Share on other sites

So many different answers for such a simple question :D This is how flexible PW is!

and I will add another one ;)

If you would like to have the kind of "settings" page that CMSs like Wordpress have, you can create a page named "site_settings" or "preferences" or "watheveryouwant", and throw there fields like "site_title", "background_color", "content_width", etc... and call them from templates like this:

$settings = $pages->get('name=site_settings');
$title	   = $settings->site_title;
$background  = $settings->background_color;
$width	   = $settings->content_width;

EDIT: I created variable $settings for readability

  • Like 1
Link to comment
Share on other sites

Wow, five responses to my question! Really impressive!

Including a variable in a config file makes sense. But from the point of view of my clients, many will question why they can't modify the site title directly in the CMS. Giving them the ability to change it and other variables, even if they won't, is really important.

Thanks for the welcome to the forum!

Link to comment
Share on other sites

  • 3 weeks later...

I think I like diogo's route and have got it working after a bit of a steep learning curve (boy am I new at this).

But I don't understand how to most efficiently use the results.

Right now I have this in a template, just to prove it's all working (it's a bit verbose for now while I am still so unsure):

$settings    = $pages->get('name=site-settings');
$pref_site_name_simple = $settings->pref_site_name_simple;
$pref_site_strapline = $settings->pref_site_strapline;
$pref_site_name_precise = $settings->pref_site_name_precise;
echo $pref_site_name_simple;
echo $pref_site_strapline;
echo $pref_site_name_precise;

Q1. If after learning how to do this I wanted to create a new template and, say, just publish the $pref_site_name_simple, would I have to use this much code?:

$settings    = $pages->get('name=site-settings');
$pref_site_name_simple = $settings->pref_site_name_simple;
echo $pref_site_name_simple;

Q2. If I created 10 templates that will need to use this data, is there a DRY (don't repeat yourself) way to include it so that if for example I suddenly decided I wanted the third line to read:

echo "This site is: "; $pref_site_name_simple;

I wouldn't have to go and edit the code in 10 places? I suppose I could use .inc file but that feels very 'low tech' and feels like I am straying far from the optimum way of using PW in this example.

Sorry these are such lame questions, I'm just keen to not begin on the wrong foot and use PW inefficiently.

Thanks a lot for any comments, cheers, -Alan

Link to comment
Share on other sites

Q1. If after learning how to do this I wanted to create a new template and, say, just publish the $pref_site_name_simple, would I have to use this much code?:

Your code looks longer mainly because your variables and field names are also very long. The same code looks shorter like this:

$settings = $pages->get(123);
$sname = $settings->simple_name;
echo $sname;
Q2. If I created 10 templates that will need to use this data, is there a DRY (don't repeat yourself) way to include it so that if for example I suddenly decided I wanted the third line to read:

You can put all your variables on "settings.inc" file, for instance, and include it on the "head.inc" file, or whichever file that you know will be included by all the templates.

EDIT: changed get('name=site-settings') to get(123) to make it even shorter ;)

Link to comment
Share on other sites

Q1:

If all you want to do is echo the contents of the field 'pref_site_name_simple' belonging to the site-settings page probably this:

echo $pages->get("/site-settings/")->pref_site_name_simple // or "name=site-settings" if you expect the page to move in levels

or if you want to make a variable for reuse:

$psns = $pages->get("/site-settings/")->pref_site_name_simple

Remember the PW api allows for chaining, and all fields are at your fingertips regardless from what template you are working.

Q2

There's nothing low-tech about an inc file. Instead it seems ideal to store stuff like you mentioned.

Link to comment
Share on other sites

Thanks SiNNuT.

Remember the PW api allows for chaining, and all fields are at your fingertips regardless from what template you are working.

I had totally missed that (boy am I new to this) but from chaining in jQuery I see what you mean here and it's music to my eyes, thanks for the advice.

Link to comment
Share on other sites

  • 2 years later...

i created a page with some of those global settings, such as: a collection of images for the banner's slideshow, the sit'e logo, its name, the footer, etc. and to make it more dynamic, i merge it with the title of the page, like so:

in the beginning of the file, before any html:

$pg_settings = $pages -> get('name=settings');

on the title:

<title><?=$pg_settings->title ." / ". $page -> title; ?></title>

and (for completion) on the footer:

<footer><?=$pg_settings->rodape?></footer>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...