I wish to display a variable that is stored in a session at the top of each page throughout my website. At the minute, on every single page, in the controller index() I have;
$data['credits'] = $this->session->userdata('credits');
I havve created a seperate view for the navigation bar (where the variable will be displayed). I have called it vNav.php. In vNav.php I then do echo $credits.
For every new view, I have to include the vNav.php, but that also means in the other view's controllers, I have to set the $data['credits'] variable in the index() function.
Is there a way in CI to do this automatically for me? So I don't have to have the same line of code in all my controllers?
Thanks
Okay here's a better design for your views.
First, create a folder called include/, in this folder create a header.php, footer.php, template.php.
header.php
<html>
<head>JS - CSS - META</head>
<body>
<div>COMMON MESSAGE</div>
footer.php
<footer>FOOTER GOES HERE</footer>
</body>
</html>
template.php
$this->load->view('include/header.php');
$this->load->view($main_page);
$this->load->view('include/footer.php');
any controller:
$data['main_page'] = "hello_world"; // view/hello_world.php
$this->load->view('include/template',$data);
So in that way, you can create a common section that will be displayed on all pages by adding it to the header or the footer. If you want, you can also create another file in the include folder and then include it in the template so that it will be loaded automatically every where by only modified one file
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.
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.
I am a newbie in Yii Framework and creating a CRM which is module based.
Using different tutorials I am able to create my own theme, but now I am stucked at one point.
In my theme, the upper <nav> and left <nav> remains the same throughout the app, until user is logged in. That's why I made it a part of my main.php, but in the login page there are no buttons to show, just simple login form with 2 textfields.
How can I implement this form in my application using custom themes?
I have tried to define a layout in that particular action but not succeeded. Any help would be appreciated.
Using a custom layout for your view is the right way to go.
You can either set the layout in the controller action or in the view.
$this->layout = "//layouts/mylayout";
Note that the default layouts column1.php and column2.php also use the main.php layout file.
Try this step by step :
Create New theme
You can create a new theme and add this to the directory
Application_Root/themes.
Look at the themes/classic directory to get an an idea of the structure of the directory.
The important file (at this stage) is :-
Application_Root/themes/views/layouts/main.php
Customise your theme contents
Copy the css, image, js files etc to the correct directory and change the main.php file to your liking. For example, if your main.php says
<link href="css/mystyle.css" rel="stylesheet">
Then you will have a file
Application_Root/css/mystyle.css
Create the content placeholder.
Somewhere in your main.php, there will be a placeholder for dynamic text, which is specified by.
<?php echo $content; ?>
Tell yii to use the theme.
Change the file Application_Root/protected/config/main.php by adding the following line just before the last line (containing the closing bracket).
'theme'=>'surveyhub'
Create the layout placeholders.
Create an HTML segment that will be written into the $contents portion of main.php. Call it for example one_column.php. The file path will therefore be Application_Root/themes/views/layouts/one_column.php In that file, where you want the dynamic text to be placed, create a placeholder.
<?php echo $content; ?>
Tell Yii to use the layout.
In the file Application_Root/protected/components/Controller.php, add or modify the layout variable to read :
public $layout='//layouts/one_column.php';
Refresh the page
I would like to display a Drupal view without the page template that normally surrounds it - I want just the plain HTML content of the view's nodes.
This view would be included in another, non-Drupal site.
I expect to have to do this with a number of views, so a solution that lets me set these up rapidly and easily would be the best - I'd prefer not to have to create a .tpl.php file every time I need to include a view somewhere.
I was looking for a way to pull node data via ajax and came up with the following solution for Drupal 6. After implementing the changes below, if you add ajax=1 in the URL (e.g. mysite.com/node/1?ajax=1), you'll get just the content and no page layout.
in the template.php file for your theme:
function phptemplate_preprocess_page(&$vars) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$vars['template_file'] = 'page-ajax';
}
}
then create page-ajax.tpl.php in your theme directory with this content:
<?php print $content; ?>
Based on the answer of Ufonion Labs I was able to completely remove all the HTML output around the page content in Drupal 7 by implementing both hook_preprocess_page and hook_preprocess_html in my themes template.php, like this:
function MY_THEME_preprocess_page(&$variables) {
if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
$variables['theme_hook_suggestions'][] = 'page__embed';
}
}
function MY_THEME_preprocess_html(&$variables) {
if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
$variables['theme_hook_suggestions'][] = 'html__embed';
}
}
Then I added two templates to my theme: html--embed.tpl.php:
<?php print $page; ?>
and page--embed.tpl.php:
<?php print render($page['content']); ?>
Now when I open a node page, such as http://example.com/node/3, I see the complete page as usual, but when I add the response_type parameter, such as http://example.com/node/3?response_type=embed, I only get the <div> with the page contents so it can be embedded in another page.
I know this question has already been answered, but I wanted to add my own solution which uses elements of Philadelphia Web Design's (PWD) answer and uses hook_theme_registry_alter, as suggested by Owen. Using this solution, you can load the template directly from a custom module.
First, I added raw.tpl.php to a newly created 'templates' folder inside my module. The contents of raw.tpl.php are identical to PWD's page-ajax.tpl.php:
<?php print $content; ?>
Next, I implemented hook_preprocess_page in my module in the same fashion as PWD (except that I modified the $_GET parameter and updated the template file reference:
function MY_MODULE_NAME_preprocess_page(&$vars) {
if ( isset($_GET['raw']) && $_GET['raw'] == 1 ) {
$vars['template_file'] = 'raw';
}
}
Finally, I implemented hook_theme_registry_alter to add my module's 'templates' directory to the theme registry (based on http://drupal.org/node/1105922#comment-4265700):
function MY_MODULE_NAME_theme_registry_alter(&$theme_registry) {
$modulepath = drupal_get_path('module','MY_MODULE_NAME');
array_unshift($theme_registry['page']['theme paths'], $modulepath.'/templates');
}
Now, when I add ?raw=1 to the view's URL path, it will use the specified template inside my module.
For others who may hit this page, if you're just working with standard callbacks (not necessarily views), this is easy. In your callback function, instead of returning the code to render within the page, use the 'print' function.
For example:
function mymodule_do_ajax($node)
{
$rval = <<<RVAL
<table>
<th>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</th>
<tr>
<td>Cool</td>
<td>Cool</td>
<td>Cool</td>
</tr>
</table>
RVAL;
//return $rval; Nope! Will render via the templating engine.
print $rval; //Much better. No wrapper.
}
Cheers!
Another way to do it which I find very handy is to add a menu item with a page callback function that doesn't return a string:
Example:
/**
* Implementation of hook_menu.
*/
function test_menu(){
$items['test'] = array (
/* [...] */
'page callback' => 'test_callback',
/* [...] */
);
return $items;
}
function test_callback() {
// echo or print whatever you want
// embed views if you want
// DO NOT RETURN A STRING
return TRUE;
}
-- Update
It would be much better to use exit(); instead of return TRUE; (see comment).
Hey, here's yet another way of doing it:
1) Download and install Views Bonus Pack (http://drupal.org/project/views_bonus)
2) Create a Views display "Feed" and use style "XML" (or something you think fits your needs better).
3) If you're not satisfied with the standard XML output, you can change it by adjusting the template for the view. Check the "theme" settings to get suggestions for alternative template names for this specific view (so you'll still have the default XML output left for future use).
Good luck!
//Johan Falk, NodeOne, Sweden
Based on answer of Philadelphia Web Design (thanks) and some googling (http://drupal.org/node/957250) here is what worked for me in Drupal 7 to get chosen pages displayed without the template:
function pixture_reloaded_preprocess_page(&$vars)
{
if ( isset($_GET['vlozeno']) && $_GET['vlozeno'] == 1 ) {
$vars['theme_hook_suggestions'][] = 'page__vlozeno';
}
}
instead of phptemplate, in D7 there has to be the name_of_your_theme in the name of the function. Also, I had to put two underscores __ in the php variable with the file name, but the actual template file name needs two dashes --
content of page--vlozeno.tpl.php :
<?php print render($page['content']); ?>
The output, however, still has got a lot of wrapping and theme's CSS references. Not sure how to output totally unthemed data...
Assuming you're in Drupal 6, the easiest way to do this is to put a phptemplate_views_view_unformatted_VIEWNAME call in template.php (assumes your view is unformatted - if it's something else, a list say, use the appropriate theme function). Theme the view results in this theme call then, instead of returning the results as you normally would, print them and return NULL. This will output the HTML directly.
PS - make sure to clear your cache (at /admin/settings/performance) to see this work.
there are probably a number of ways around this, however, the "easiest" may be just setting your own custom theme, and having the page.tpl.php just be empty, or some random divs
// page.tpl.php
<div id="page"><?php print $content ?></div>
this method would basically just allow node.tpl.php to show (or any of drupal's form views, etc...) and would be an easy way to avoid modifying core, or having to alter the theme registry to avoid displaying page.tpl.php in the first place.
edit: see comments
ok i played around with views a bit, it looks like it takes over and constructs it's own "node.tpl.php" (in a sense) for display within "page.tpl.php". on first glance, my gut feeling would be to hook into theme_registry_alter().
when you're looking at a views page, you have access to piles of information here, as well as the page.tpl.php paths/files. as such i would do something like:
function modulejustforalteration_theme_registry_alter(&$variables) {
if (isset($variables['views_ui_list_views']) ) {
// not sure if that's the best index to test for "views" but i imagine it'll work
// as well as others
$variables['page']['template'] = 'override_page';
}
}
this should allow you to use a "override_page.tpl.php" template in your current theme in which you can remove anything you want (as my first answer above).
a few things:
as i said, not sure if views_ui_list_views is always available to check against, but it sounds like it should be set if we're looking at a view
you can alter the theme paths of the page array if you prefer (to change the location of where drupal will look for page.tpl.php, instead of renaming it altogether)
there doesn't appear to be any identifiers for this specific view, so this method might be an "all views will be stripped" approach. if you need to strip the page.tpl.php for a specific view only, perhaps hooking into template_preprocess_page() might be a better idea.
I like the Drupal module. BUt, here's another way.
copy page.tpl.php in your theme folder to a new file called page-VIEWNAME.tpl.php, where VIEWNAME is the machine-readible name of the view.
Then edit page-VIEWNAME.tpl.php to suit.
There is also http://drupal.org/project/pagearray which is a general solution...
Also, #Scott Evernden's solution is a cross site scripting (XSS) security hole. Don't do that. Read the documentation on drupal.org about how to Handle Text in a Secure Fashion http://drupal.org/node/28984
A simple way to display content of a special content-type you wish to display without all the stuff of the page.tpl.php:
Add the following snippet to your template.php file:
function mytheme_preprocess_page(&$vars) {
if ($vars['node'] && arg(2) != 'edit') {
$vars['template_files'][] = 'page-nodetype-'. $vars['node']->type;
}
}
Add a page-nodetype-examplecontenttype.tpl.php to your theme, like your page.tpl.php but without the stuff you don't want to display and with print $content in the body.
If I understand your question, you want to have nodes which contain all the HTML for a page, from DOCTYPE to </HTML>. What I would do is create a content type for those nodes -- "fullhtml" as its machine-readable name -- and then create a node template for it called node-fullhtml.tpl.php. You can't just dump the node's contents, as they've been HTML-sanitized. node.fullhtml.tpl.php would literally be just this:
echo htmlspecialchars_decode($content);
Then you'll need a way to override the standard page.tpl.php. I think what you could do is at the top of your page.tpl.php check the $node's content type, and bail out if it's fullhtml. Or, set a global variable in node-fullhtml.tpl.php that page.tpl.php would check for.
I'm no Drupal expert, but that's how I'd do it. I'm talking off the cuff, so watch for devils in the details.
I see you have already gone and made yourself a module, so this may no longer help, but it is fairly easy to get a view to expose an rss feed, which might be an easier way of getting at the content, especially if you want to include it on a different site.
On D7 you can use menu_execute_active_handler
$build = menu_execute_active_handler('user', FALSE);
return render($build);
jeroen's answer was what did for me after playing with it. I have a Drupal 7 site.
First of all make sure you replace MY_THEME with your theme name. Yes it is obvious but most newbies miss this.
I actually already had a function MY_THEME_preprocess_page(&$variables) {. Do not recreate the function then but add this code at the end of the function before you close it with }.
if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
$variables['theme_hook_suggestions'][] = 'page__embed';
}
My function used $vars not $variables, so I had to update that as well. Again obvious if you think look for it.
My first answered allowed me to only display the node when I called it up in a web browser. However the ultimate goal of this is to embed the drupal node in an 3rd party site using iframe.
Since the release of Drupal Core 7.50 iframe is by default blocked to prevent Clickjacking
To get only the node to successfully embed in a 3rd party site you also need to override the x-frame default setting. Everything started working after I added the following in template.php
function MY_THEME_page_alter($page) {
if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
header_remove('X-Frame-Options');
}
}