Displaying another controller's view inside a view - php

I'm using cakephp and I want the login view for the controller 'users' to be displayed on the default.ctp for the layout controller. I can I do this?
For example:
<div id="leftNav">
<div id="login-block" class="block">
<?php
//render users/login here
?>
<ul>
<li>Login</li>
<li>Register</li>
<li>Logout</li>
</ul>
</div>
</div>

I would make it an element - they are designed for re-usable code chunks that can be used anywhere.
To summarize the functionality, in your view you'd use:
<?php echo $this->element('login'); ?>
and put your "login-block" div login stuff in
/app/views/elements/login.ctp

If you don't use elements for that and you already have the login view you can use it like this
<?php echo $this->requestAction('/users/login'); ?>

Related

PHP conditional HTML not working

I'm struggling to get this PHP conditional statement to work, I have ensured that the session variable ID is not set by using session_destroy(), however no matter whether the ID variable is set or not the dropdown menu is displayed rather than the login button:
<nav>
<ul>
<li>Home</li>
<li>Catalogue</li>
<?php if(isset($_SESSION['ID']) && $_SESSION['ID'] != ""): ?>
<div class="dropdown">
<button class="dropbtn">Profile</button>
<div class="dropdown-content">
Overview
Edit Details
Logout
</div>
</div>
<?php else: ?>
<li>Login</li>
<?php endif; ?>
<li>Contact Us</li>
</ul>
</nav>
My desired outcome is that when the session variable ID is set, the dropdown menu is visible, otherwise the login button is visible.
Your code should work.
session_destroy() doesn't destroy the session cookie and the globals related to the current session.
Try again using session_unset() to clear all the variables related.

How to make navbar constant across multiple pages?

I have looked at prevous questions about this and people say php and have not found an answer. how do I convert my navbar to php and use it in multiple html pages. Could someone tell me how to do so? I am currently taking a codecademy course on php and it is really hard for me to understand so please be patient.
<div class="tabs">
<ul>
<a href="http://degraphic-design.dunked.com/contact-me" style="text-decoration:none">
<li class="contact">Contact</li>
</a>
<li class="dropdown">Shop</li>
<li class="forum">Forum</li>
<a href="index.html" style="text-decoration:none">
<li class="about">About</li>
</a>
<li class="team">Team</li>
<a href="http://degraphic-design.dunked.com/" style="text-decoration:none">
<li class="portfolio">Portfolio</li>
</a>
</ul>
</div>
Say you have about.php and home.php in the root of your website. Create a directory called partials (or whatever), go into partials and put the contents of your navigation HTML in a file called nav.php. Then in about.php and home.php, use this where you want to include the navigation code
<?php include 'partials/nav.php'; ?>
Here is one way (extremly basic):
Create a PHP file called index.php
<!DOCTYPE html>
<html>
<body>
<header>
<?php
include 'header.php';
/**
* say you wanted a different header for shop
* if($_GET['page'] === 'shop') {
* include 'header-shop.php';
* } else {
* include 'header.php';
*}
*/
?>
</header>
<div id="main">
<?php
include $_GET['page'].'.php'; // assuming your storing your pages in same path as index
?>
</div>
<footer>
<?php
include 'footer.php';
?>
</footer>
</body>
</html>
Then a header.php
<div class="tabs">
<ul>
<li>Contact</li>
<li>Shop</li>
</ul>
</div>
And create your page files contact.php, shop.php ect.
Updated to a slightly more elaborate example to give you the idea.

Drupal - Views - Taxonomy Term ID - custom template - anchors

Quick version: How do I output a taxonomy term ID in a views template? I just want the numeric ID value. This will be used as a link anchor.
Long version:
Scenario, I have a view which displays a list of taxonomy terms. That view has a Page and a Block. The Page view is set to display the Block view as a header. That Block view simply contains the taxonomy names. The Page view displays all of the taxonomy content.
I want the Block view list to anchor to the items in the Page view:
This view is already built, the missing part of the equation is getting the anchor links in place.
The view currently comprises 3 custom template files:
views-view-fields--categories--page.tpl.php
<article id="NEED THE TERM ID HERE">
<header>
<h2 class="flag-heading"><?php print $fields['name']->content; ?> <span>in association with <?php print $fields['field_sponsor']->content; ?> </span></h2>
</header>
<div class="table">
<div class="table-cell">
<?php print $fields['field_category_image']->content; ?>
</div>
<div class="table-cell">
<?php print $fields['description']->content; ?>
</div>
</div>
</article>
views-view-fields--categories--block.tpl.php
<li><?php print $fields['name']->content; ?></li>
views-view--categories--block.tpl.php
<ul>
<?php print $rows; ?>
</ul>
I've tried using a views contextual filter rewrite on the top block view links, with no luck.
All I need is the variable for the TERM ID - I've done a var dump of the available variables, I can see the TID in that list, but have no idea how to reference it in a views-view-fields template file and can find nothing online that answers this most simple of concepts.
Screenshots of the Page and Block view setup:
Finally won my argument that this jump list is completely redundant and stupid, so it'll be removed, however, I did manage to output the TID, which was fairly obvious, as these things often are...
views-view-fields--categories--block.tpl.php
<li>
<?php print $fields['name']->content; ?>
</li>
views-view-fields--categories--page.tpl.php
<article id="cat<?php print($view->result[$view->row_index]->tid); ?>">
<header>
<h2 class="flag-heading"><?php print $fields['name']->content; ?> <span>in association with <?php print $fields['field_sponsor']->content; ?> </span></h2>
</header>
<div class="table">
<div class="table-cell">
<?php print $fields['field_category_image']->content; ?>
</div>
<div class="table-cell">
<?php print $fields['description']->content; ?>
</div>
</div>
</article>
The variable is obviously in the view array, it was just a case of getting the index of the current view item.
Adding
<?php print $fields['tid']->content; ?>
should give you the TID in views-view-fields--xxx--page.tpl.php and --block.tpl.php
Make sure the fields are set to remove any default wrappers and you should be good to go.

php included navigation menu not working

I have a php page that has many includes. The whole page runs fine, but for my navigation bar, it does not open any page when I click on it. Please, what is missing here. Any help will be appreciated. many thanks
Here are my codes for the includes
<div id='cssmenu'>
<ul>
<li class='active'><span>Home</span></li>
<li><span>About Us</span></li>
<li><span>Packages</span></li>
<li><span>Partners</span></li>
<li><span>Gallery</span></li>
<li class='last'><span>Contact Us</span></li>
</ul>
</div>
And here is my main page code:
<body>
<div id="wrapper">
<div>
<img src="images/banner.png" width="940" height="200" />
</div>
<?php include('includes/nav.php'); ?>
<?php include('includes/slider.php'); ?>
<?php include('includes/nav.php'); ?>
<?php include('includes/contents.php'); ?>
<?php include('includes/sidebar.php'); ?>
<?php include('includes/footer.php'); ?>
</div> <!-- End #wrapper -->
</body>
Not sure of your file structure, but try removing the '../' on all you links
Change this:
<li class='active'><span>Home</span></li>
to this:
<li class='active'><span>Home</span></li>
Take the ../ out of your path.
When you include the nav.htm to your main page it acts as if it is in the main folder
Therefore if you use '../' it won't find it.

how to create widget in Yii based on existing login action view?

how to create a widget out of an existing login action view ?
this is what I currently have in my login.php view file of the site's controller actinLogin()
<div id="login-wrapper">
<div class="login-container">
<?php $form=$this->beginWidget('CActiveForm', array('id'=>'login-form','enableAjaxValidation'=>true,)); ?>
<div class="login-input">
<p>
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('placeholder'=>'username')); ?>
</p>
<p>
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('placeholder'=>'password')); ?>
</p>
<div id="bmenu">
<ul class="menu">
<li class="register"><?php echo CHtml::link("Registration", array('wsmembers/register'));?></li>
<li class="login"><?php echo CHtml::submitButton('Login',array('id'=>'login_button')); ?></li>
</ul>
</div>
</div>
<?php $this->endWidget(); ?>
</div><!-- .login-container -->
<div class="login-bg-bottom"></div>
if I click the default login link from the navbar, that's the only time that code above shows
the login box at the upper right corner of the page
but the problem is, the login box should be at the homepage and must already be there
without clicking a login link at the navbar. so how am i gonna do that? this have something to do with main.php layout file right?
Indeed.
What you have defined is a view that is used for content. If you click the link the login action will be executed and it it will show the form.
If you want this to be shown always, just do a Yii::app()->controller->renderPartial on the view. Note that it cannot be $this->renderPartial as you would normally do since the main layout is not executed by the controller. I usually define an alias called "userViews" that points to protected/views so I can do:
Yii::app()->controller->renderPartial('userViews.site.login');
Or something like that. Hope that helps :)
As for the alias you can add this to your config:
Yii::setPathOfAlias('userViews', dirname(__FILE__) . '/../../protected/views');
This is assuming your views are indeed under protected views.

Categories