Theming and layout in yii framework - php

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

Related

How do I get my current wordpress theme's header & footer in a PHP file?

The file is manually created by me & not a part of the theme.
I've tried by placing get_header( ) inside my file but it shows a error that get_header( ) is not defined.
Maybe you don't have default header and footer names. Then you need to set additional name.
Includes the header.php template file from your current theme's directory. If a name is specified then a specialised header header-{name}.php will be included.
If the theme contains no header.php file then the header from the
default theme wp-includes/theme-compat/header.php will be included.
Source: https://codex.wordpress.org/get_header
I highly discourage it but you need to include the wp-load.php file that resides in the root of your wordpress installation, in your lone php file. That is how you have access to WP functions and capabilities.
But again I HIGHLY DISCOURAGE IT. Find another way, create a simple plugin and activate it, in which you put that php file's code, or if part of a theme, just create a new page template with a different header, that you can call with get_header('name_of_header_without_php_extension') or a new footer called the same way but with get_footer().
There are alternatives, maybe if you share what you are trying to achieve, we can guide you to a better, safe solution.
Based on theme it's defer.
Same time define as get_header(),get_footer()
But now latest themse assign particular theme name based header & footer function
Ex: Theme Name is "Demo"
So the header function assign as Demo_get_header() or get_header_Demo()
Dashboard ->Appearance ->Editor -> in your Template file or in page.php write
<?php get_header()?>,<?php get_footer()?>
if you want to use different headers on different pages
1. create new file , name it header-example.php
2. you can call this header in any page using code

Wordpress Adding Custom HTTP Header To One Template File

I need to add a header Access-Control-Allow-Origin: * into one of my template files. How would I do this I would like it to be for just this specific template so that its not site wide,
I think you're looking for is_page_template();
<?php
if(is_page_template('your-page-template.php')) { //Change this to the path to your wordpress template
//Add your code here...
} ?>
you can create a custom template page and call the custom Header on this template.
To create a page and apply a page template
use this code to create page template
First you need to have a single page. There are several ways to do it, one is to create a page in wordpress, then use the ID and create a file page-ID.php in the theme folder. Other way is to create a template file which you can select from the side menu in the wordpress editor.
In that page, you can add the php header and the code you want to show.

Wordpress add .php file manually

I have a .php file which uses some WP functions e.g. get_stylesheet_directory_uri(), query_posts, etc... and $wpdb of course.
File it self return JSON data so it is not intended for viewing on its own.
I'm not looking for template, or clicking around in WP dashboard, I just want to know where is the good place in WP file structure to put the .php file and expect WP functions and object to be available.
And also what do I need to include at the top?
You can create custom template in theme root directory or in child theme (child theme is important when you are creating custom template).
template should contain template name, header and footer function.
eg. custom-tpl.php
<?php
Template Name: My Custom Temlate
get_header();
//your other functions and content goes here
get_footer();
after creating template it will appear in your edit page template section under drop down. select custom template for your page and view page you will get your output
You can create a custom page template.
Inside the page template:
<?php
global $wpdb;
[here you will write your code]
?>
On the front pages where you want the json output, use this url
http://domainname.com/wp-content/themes/themename/custompagetemplatename.php
Use your domain name (domainname), theme name (themename) and theme template file name (custompagetemplatename) in place of example names in the url. This url will return you the output for your code.
About this comment in your template:
<?php
/* Template Name: Full Width Page */
?>
You do not need a template name in the header of your file unless you want to use this as a template for pages in wordpress.

Custom Page Template VS Plugin VS Shortcode refer to function

I need to create a form that will be called many times through application .
below is the steps i did :
1- Create a form.php page and place it in my child theme folder.
C:\wamp\www\test\wp-content\themes\designfolio-child
i wrote the below code at the top of the page
<?php /* Template Name: Hercal Template */ ?>
and then i write my form code.
2-This page calls another PHP files and Jquery file , i place those file in
C:\wamp\www\test
3- I create a wordpress page and select a template option 'Hercal Template' ,
The page run now ,form loaded and working fine but i have some issue that i need to understand.
1-What about security , is it secure to place files and database connection to test folder directly ?
2-How can i call my form inside other pages ?
3-the above steps enables me to create a template page , so what is the difference between template page , plugin , short code that refer a function ???
I mean :
when i create a plugin , i can call it anywhere by using function name.
when i create a function,place it into wp-content and create shortcode for it , i can call it anywhere by using shortcode .
so what is the difference between them , i have conflict between (page template,plugin,shortcode).
Thanks
You should use shortcode to display form.
if you will use template than you can't put extra content/design for multiple places.
if you will use function than you have to call that in your page or you need to physically edit template file.
if you will use your form as shortcode than you can access/call it anywhere in your page/post/custom post and place it on anywhere between the div, paragraph etc.
Even you can access it on php file as well
I suggest you to use short code

CI - Public Variable for all pages?

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

Categories