Displaying a drupal page without the template around it in Drupal 7 - php

I'd like to render the contents of a "basic page" in drupal. Something like this question: displaying a Drupal view without a page template around it but for Drupal 7.
My attempt almost works:
function mytheme_preprocess_page(&$variables, $hook) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$variables['theme_hook_suggestions'][] = 'page__ajax';
}
}
And have a file named page--ajax.tpl.php in the same directory where template.php lives:
<?php print $page['content']; ?>
The problem is that it still renders the menu and my two custom blocks from the sidebar. I only want the page content. What should I change?

You are almost there. The only thing you need is to add a custom HTML wrapper template.
Add a function to template.php:
function THEMENAME_preprocess_html(&$variables, $hook) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$variables['theme_hook_suggestions'][] = 'html__ajax';
}
}
Create a file named html--ajax.tpl.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php print $styles; ?>
<?php print $scripts; ?>
</head>
<body class="<?php print $classes; ?>">
<?php print $page_top; ?>
<?php print $page; ?>
<?php print $page_bottom; ?>
</body>
</html>
Flush all caches. That's all.

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.
Shamelessly taken form here :
displaying a Drupal view without a page template around it (second best answer for drupal 7).
Alexei solution still use the page template wich is in charge of displaying blocks

Related

Retrieve session content from DB if session value is not set with PHP

Im generating a menu dinamically from the database using a DB function. This function depends on the user profile. So when a user login, the function will paint the menu content like this:
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
The problem is that each time I access to one of the pages, the php code calls the DB function, so I want to store the menu content on a _SESSION value and use it until session finish.
So, I try this. If $_SESSION['showmenu'] is empty call the DB function showmenuweb() and assign the menu content to $_SESSION['showmenu'] and if its not empty then, just $_SESSION['showmenu']; content.
if(!isset($_SESSION['showmenu'])) {
echo "there is no menu on session";
$_SESSION['showmenu'] = showmenuweb('ADMIN');
} else {
echo "there is menu on session";
echo $_SESSION['showmenu'];
}
Then I call the session content with:
<php
echo $_SESSION['showmenu'];
?>
But this code always call the function showmenuweb(), its looks like always the $_SESSION['showmenu'] is empty.
Any help will be apreciated.
UPDATED: This is the code:
<?php
include_once('init.php');
if(!isset($_SESSION))
{
session_start();
}
echo $_SESSION['showmenu'];
if(!isset($_SESSION['showmenu'])) {
echo "there is no menu on session";
$_SESSION['showmenu'] = showmenuweb('ADMIN');
} else {
echo "there is a menu on session";
}
?>
<!DOCTYPE html>
<html lang="en">
<body>
<?php
echo $_SESSION['showmenu'];
echo '<br />';
echo '<br />';
showmenuweb('ADMIN');
?>
</body>
</html>
The html output shows:
there is no menu on session
menu1
menu2
menu3
menu1
menu2
menu3
I fix the problem by using two session variables.
First assign the db value $_SESSION['MENU'] = $row['SALIDA'];
Then I check it.
if(!isset($_SESSION['MENU'])) {
$_SESSION['SALIDA'] = showmenuweb('USER');
}
Thanks to all for the help.

No navigation bar displayed after log in

I have 3 different navigation bars, a different 1 has to be loaded depending on the authority of the user. I have made a function in a different php file so i re use the code in each page. My problem is that after logging in , no navigation bar is being loaded. any suggestions to what might be the problem ?
Code in function.php file
function checkAuth()
{
session_start();
if(empty($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
include('regnav.php');
}
else if ($_SESSION['role'] =="admin"){
include('adminnav.php');
}
}
code in the begging of each page
<?php
require_once("function.php");
checkAuth();
?>
Try passing the session result into the function:
place session call at the top of the rendering page
session_start();
$this_session = $_SESSION['role'];
require_once("function.php");
place where you want to render menu
checkAuth($this_session);
the function
function checkAuth($this_session)
{
if($this_session == ''){include('menu.php');}
elseif($this_session == 'registered'){include('regnav.php');}
elseif($this_session == 'admin'){include('adminnav.php');}
}
I would personally restructure your pages just a little, like this:
At the top of each page put:
session_start();
include('function.php');
checkAuth();
Change the function to:
function checkAuth()
{
if (!isset($_SESSION['role'])) { require_once('menu.php'); }
if ($_SESSION['role'] == 'registered') { include('regnav.php'); }
if ($_SESSION['role'] == 'admin') { include('adminnav.php'); }
}
This assumes you aren't using functions in your navbar files, since you don't call any functions to write the navbar. You could, of course, put both navbars into functions and put both functions into a single navbar.php file that you would include at the top of every page (or put the functions into function.php), then call the appropriate navbar function from the checkAuth() function, like this:
if ($_SESSION['role'] == 'registered') { navbar_registered(); }
if ($_SESSION['role'] == 'admin') { navbar_admin(); }

Yii, I am trying to have a dynamic layout for my views

I want different views to render using one layout(sections.php), but the layout should load a different background for each view. How can i achieve this without using 4 different layouts for each view.
protected/views/site
- index.php
- about.php
- product.php
- brands.php
protected/views/layout
- sections.php
<html>
<head></head>
<body>
<div id= "viewname">
<ul>
<li>Home</li>
<li>about</li>
<li>products</li>
<li>brands</li>
</ul>
<!--I want to load different bg for this div for each view--->
<!--there's a lot of content that comes here, i dont want to do it in every page--->
<!--how can i load the class name for this div dynamically if possible-->
</div>
<div class=section-cont>
<?php echo $content; ?>
</div>
</body>
</html>
my css
#about
{
....
background-image: ('../img/about.jpg')
}
#product
{
....
background-image: ('../img/product.jpg')
}
#brands
{
....
background-image: ('../img/brands.jpg')
}
myController
class SiteController extends Controller
{
public function actionAbout()
{
$this->layout = "sections";
$this->render('about');
}
//functions for other views is similar to above
sounds like you want to change background depending on what page. The way I'd do this is instead of creating a view for each page. Set a variable for in the body tag for the class in your sections layout. Such as:
<body class="<?php echo $body_class; ?>">
You'll then be able to pass the body class into your view from your Controller Action. Like So:
$this->render('about',array('body_class'=> 'about-page'));
You'll then be able to target the page and change the background image by targeting the class in your CSS.
Hope this helps.
EDIT: Question required passing variable to layout
Sorry - confused myself on layout vs view file.
Layout will have access to $this which will be the current controller.
So in your controller if you add a public property/variable land edit the action like so:
class SiteController extends Controller
{
public $body_class;
public function actionTest()
{
$this->layout = "testlayout";
$this->body_class = 'test-page';
$this->render('test');
}
And then change the body class or div class in the layout file to $this->body_class
Note: you could also assign $this->body_class in the page view if you prefer it that way.
If i understand well your question, you can do it by this.
1.take current page name.
2.for each page store another value.
3call that class where you want .
<?php
$page=basename($_SERVER['PHP_SELF']);
if($page == 'Home') {
$css="#Home";
}
if($page == 'about') {
$css="#about";
}
if($page == 'products') {
$css="#product";
}
if($page == 'brands') {
$css="#brands";
}
?>
<div id="<?php echo $css ?>" class="section-cont"> </div>

(WordPress) How to use if OR if post?

I want to show some HTML code for specific posts, so I put this on HEADER templete:
<?php if(is_single(24) || is_single(34)) { ?>
MY HTML CODES
<?php } ?>
I tried those too:
<?php if(is_single(24) && is_single(34)) { ?>
<?php if((is_single(24)) && (is_single(34))) { ?>
<?php if((is_single(24)) || (is_single(34))) { ?>
And its not working. If I put this code for single post, like this:
<?php if(is_single(24) { ?>
Its working well.. but I need to do that for many posts.
You could do if (is_single() && in_array($post->ID, array(24, 34)) {}
Depending on context, you may need to make $post global.
I'd probably add some metadata to the posts though and check for that instead... Less messy.
Try using in_array
<?php if(in_array(POST_ID, array(POST_IDS))) { ?>
MY HTML CODES
<?php } ?>

Dynamic Titles in my codeigniter header.php

I have a header.php I'm loading in my controllers for every page. However I want to have dynamic titles for each page. My idea was to pass a $title variable into the view as I'm loading it:
//Home Controller
function index()
{
$data['title'] = "Dynamic Title";
$this->load->view('header', $data);
$this->load->view('layouts/home');
$this->load->view('footer');
}
and then check for the $title variable in my header.php
<title>
<?php if ($title)
{
echo $title;
}
else
{
echo 'Default Title';
}
endif; ?>
</title>
However this doesn't work and I get a blank page. I think it is my syntax for the header.php but I can't figure out why.
Proper if Syntax
Your syntax on the if-statement is a bit off. You can use either:
if (condition) {
// do a
} else {
// do b
}
Or
if (condition) :
// do a
else :
// do b
endif;
You seem to have transposed the ending of the latter onto the former.
Using the Ternary Operator in Title
Once you've made that change, your title can be printed as easily as:
<title><?php echo isset($title) ? $title : 'Default Title' ; ?></title>
Alternative View Loading
Another method of loading views is to work with a single template file:
$data['title'] = 'Foo Bar';
$data['content'] = 'indexPage';
$this->load->view('template', $data);
This loads the template.php file as your view. Within this file you load your subsequent parts:
<?php $this->load->view("_header"); ?>
<?php $this->load->view($content); ?>
<?php $this->load->view("_footer"); ?>
By no means is this necessary, but it may help you maintain brevity in your controller.
Well I would try doing a var dump of $title in the view, just to see if it's getting passed at all.
Also, you don't need "endif;" since you're ending the if statement with the last curly brace.

Categories