CakePHP: How do I change page title from helper? - php

I'm using a helper for static pages to add a part to the title on every page.
Currently I have the following code at the top of every static page:
<?php $this->set('title_for_layout', $title->output('Nyheter')); ?>
The purpose of $title->output is to append " :: MY WEB SITE NAME".
This works fine, but for simplicity I would rather just call:
$title->title('Nyheter');
At the top of every page to set the title. The problem is that I can't call $this->set() from within the helper. Is there a way to something like this or am I completely on the wrong path here?

At the risk of being too obvious, why do you need the helper? I usually include this kind of title like:
<title><?php echo $title_for_layout . ' :: MY WEB SITE NAME' ?></title>
Plug this right into the layout and you have dynamic and static components the render nicely. For an added twist, you can filter out the " :: " if no $title_for_layout value exists. Then all you have to worry about is setting the dynamic portion on any page that needs it.

Related

Wordpress dynamic url for same page template

I have created page template in wordpress. right now its url is static like http://localhost/wordpress/test-test1 but i want to make it dynamic like http://localhost/wordpress/test1-test-test2 . Is it possible to create a single page template having dynamic url??
I want both url's to comes on single page. If page templating approach is not good then what could be other approaches. Here the test urls:
http://localhost/wordpress/test-test1
http://localhost/wordpress/test1-test-test2
http://localhost/wordpress/test13-test2-test1
I found similar links but none of the link helped me.
Here it depends on how would you change to pass argument, Suppose the last stage and it should be same always, and let page name 'anything'
<?php
add_action('init', 'add_my_rule');
function add_my_rule()
{
global $wp;
$wp->add_query_var('args');
add_rewrite_rule('test\/laststage\/(.*)','index.php?pagename=Pagename&args=$matches[1]','top');
Apply your custom template to this page, & on that template use this:
//if you visit http://.../test/laststage/name/AnyName, thus this $params will be name/AnyName.Now need to explode this and get the value.
$params = get_query_var('args');
Here's info: http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Dynamic title depending on page (DRUPAL 7)

Before I made the switch to Drupal (previously just static HTML), I had a div title in my header that changed depending on what page the user was on using the following code:
ABOUT PAGE
<?php
$title = "ABOUT US</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path); ?>
The reason this worked, however, was because I was using static HTML... which means I had every page set to use its own Index.PHP, located at (for the About page, for example) public_HTML/about/index.php. Therefore, I could edit each page individually to my liking by editing its respective Index file...
Now, with Drupal.. instead of having so many Index.PHP files like I did before (which was messy IMO...), I want to have every page use the same page.tpl.php (with the exception of the front page, which uses page--front.tpl.php).
Can I somehow modify the above PHP code to display a different title using IF statements depending on what page the user is on?
For example... (I have no idea how to PHP code so this is just to give you experts an idea of what I would want..)
Ifpage=about, $title=About Us;
Ifpage=contact, $title=Contact Us;
Could this code all be in the same page.tpl.php?
Thank you so much!
Drupal 7 has a title field for each content type. Why don't you use that?
It shows by default and you can change it for every node.
It is this code in your template file.
<?php print render($title_prefix); ?>
<?php if ($title): ?>
<h1 class="title" id="page-title">
<?php print $title; ?>
</h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
$title_prefix (array): An array containing additional output populated by modules, intended to be displayed in front of the main title tag that appears in the template.
$title: The page title, for use in the actual HTML content.
$title_suffix (array): An array containing additional output populated by modules, intended to be displayed after the main title tag that appears in the template.
See https://api.drupal.org/api/drupal/modules!system!page.tpl.php/7 for all the variables available in your page.tpl template file.
If you want to style one part different from the other you could use an extra field for it.
Or if you need to do something with the value (I wouldn't suggest this!!!).
switch ($title) {
case "About Us":
// Do something with the title
break;
case "Contact Us":
// Do something with the title
break;
case 2:
// Do something with the title
break;
}
Here's a simple approach:
Add a new field for the content type
Call it title2
Render the two fields
Use css to inline align them and add styling
Here was my solution since I am unable to position a Drupal-based title block right next to a non-Drupal based logo DIV..
I created a div called .headertitle and positioned/styled it how I want.
My header.php is an PHP include file which is called by each page and the headertitle has a PHP condition..
<?php if(empty($headertitle)) $headertitle = "ASK REAL QUESTIONS, <br><span class='white'>GET FREE ANSWERS.</span>";
echo $headertitle; ?>
That way, if no text is declared for the .headertitle, it takes the default form of "ASK REAL QUESTIONS, GET FREE ANSWERS."
...Now, I had to figure out a way to actually declare text for the .headertitle depending on what page I was on...
I did this by using a custom template for each page (node) by using this file.. "page--node--nodeid.tpl.php".
So, for my ABOUT US page, I created a tpl.php file called "Page--node--7.tpl.php".. for this page, the 7 is the nodeid which is easy to find.
Then, I added the following PHP code to declare text for the .headertitle specifically on the ABOUT US page which looks like this...
<?php $headertitle = "ABOUT<span class='white'>.US</span>"; include('includes/header.php'); ?>
DONE AND DONE.
I'll do that for each page. :)

Codeigniter session does not work after including Controller in view

I was working on a site in which on right side I am displaying some fixed type of dynamic content like event calendar, login, register etc. right side bar name is right_view.php
So first I was doing like this that I was sending parameters in every function of controller's and then in my view I was accessing right side parameters by calling right view like this
<?php $this->load->view('right_view');?>
then after login I can get my username that is stored in session.
After that I thought it is not a good approach to send parameters in every functions I just make a controller named right.php and in this controller I am passing parameters to right_view.php and after that in my view I changed my code for callig righr_view like this
<?php include(base_url().'right');?>
It display right content as I do above but one changed happen that I cannot access any of session stored variable in right side bar.
Is session does not work after including controller in view?
You're basically wanting to do a template system but going about it the wrong way. And for the record no I don't think you can (or at least should) be loading controllers into views like that.
What you want is a template file something like this:
<?php
$this->load->view('templates/header', $title);
$this->load->view('templates/sidebar',$sidebar_content);
$this->load->view('pages/'.$main_content);
$this->load->view('templates/footer');
?>
Call that template.php
Then in your controller you'd do something like this:
public function welcome()
{
$data['main_content'] = 'welcome';
$data['title']='Welcome to REfficient.com!';
$data['sidebar_content'] = 'sidebar/no_sidebar';
$data['additionalHeadInfo'] ='';
$this->load->view('templates/template',$data);
}
So if you look at the template file the first line is loading the header and including the title variable to insert into the page (header, sidebar, maincontent and footer are all their own separate PHP pages) and so on.
Now what I did (since my layout was very similar to yours) is my main sidebar file has an if statement that says if logged in show x, if not show login form.
Note - the additionalHeadInfo variable is so I can have includes like jQueryUI or something on an individual page without loading it on pages that don't need it.

Composite views with Codeigniter?

For my Codeigniter site, I started by making a view for each controller situation. This was impractical, as it would require going back to the code for each to make a change. So I changed approach and operated on a 'default' controller with optional fields. I then thought I could load special views as needed into it.
I put together this view with optional fields with fields for $title, $search_bar on/off etc. However, now came the content area. I was able to load more views into this default view using:
$data['content_views'][]='blocks/login';
$this->load->view('default/a', $data);
and in the 'default'view:
if(isset($content_views)&& (is_array($content_views)))
{
foreach($content_views as $content_view)
{
$this->load->view(&$content_view);
}
}
(and that works fine)
Two questions:
Am I making things to complex? Is this an accepted way of doing this? Or have I misunderstood the functioning of a view and how they are intended to work?
I want a way to mix the $content_view, i.e. a block of text, then a view. I'm not quite sure as to how to proceed. Say I want a message first, then a view, then more text. This method will only accept views.
Can anybody help me create this flexible approach?
Yeah I would say you're making things a little complex. While I may not be following your description well enough to know precisely how to respond, I can tell you how I do it:
First the whole site is run through a template so the header and footer are the container file and all views needed within the site are rendered as page type views - like an article page, a gallery page, etc. Components are loaded and passed to the views as strings:
$content['sidebar'] = $this->load->view('components/sidebar', $data, true);
That last true says to render as string.
Essentially, this means the page views are pretty much html with php echoing out the necessary elements. No views calling other views, which is how I read your example.
CI loads views progressively, so your controller can output like so:
$this->load->view('header', $header_data);
$view_data['sidebar'] = $this->load->view('components/sidebar', $sidebar_data, true);
$this->load->view('content', $view_data);
$this->load->view('footer', $footer_data);
and in content view, handle the sidebar like so:
<?php if(isset($sidebar)): ?>
<nav>
<?php echo $sidebar; ?>
</nav>
<?php endif; ?>
And, assuming you populate those arrays for each view it will render header, then content, then footer. And also render sidebar if it is present.
So combining everything, I'm basically saying you can load in sections in your controllers progressively, passing sub-views as strings to whichever section makes sense. That keeps your view controlling in the controller where it belongs and not in the view files themselves. In my experience, I have not had to write a site that was so complex that this construct wasn't perfectly suitable if the site is planned well.

Accessing $_SESSION from outside of Joomla

I used Joomla 1.5 for a site that I developed for a gaming company. Part of the site consists of a character generater for the game that they developed. The issue is that users want to be able to print the character sheets off, without having the Joomla template surrounding it.
As for the specifics, I have the directphp extension installed, and the entire generator is written in PHP (with a little JavaScript to handle things that PHP can't). As the generator spans several dozen page calls, it made sense to store everything in $_SESSION. All of this works correctly. In an attempt to make the final sheet printer friendly, I tried redirecting the user to a page outside of Joomla (though on the same server, and even within the same folder) but I cannot access the $_SESSION data from this new page.
I have seen several posts (a few on this site) that point to loading the Joomla Framework and accessing it that way, which I have tried, but the data that I was looking for does not appear to be contained there. Has anyone come across this problem before, or know how to get to that data?
You are making this WAY harder than it needs to be. You don't have to write any additional code to accomplish what you are trying to do. In order to print our the component output without all of the Joomla template, you just append ?tmpl=component to your URLs and Joomla will display only to component output without any of the template. If you want to give it a custom stylesheet or anything special, you can also add in a template override by adding a file named component.php in your template folder.
In order to control the CSS per page, you can add Page Class Suffixes in the menu items. Then add this code to index.php so you can use them.
Somewhere in the head add this:
$menu = &JSite::getMenu();
$active = $menu->getActive();
$pageclass = "";
if (is_object( $active )) :
$params = new JParameter( $active->params );
$pageclass = trim($params->get( 'pageclass_sfx' ));
endif;
Replace your body tag with this:
<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">
Any page that you do not specific a Page Class Suffix for will use default as the body ID, any that you do will use what ever you specify.

Categories