I'm new to Yii but I want to learn the best practices.
For example, I have the following HTML:
<html>
<head></head>
<body>
<!-- begin header -->
<div id="header"></div>
<!-- end header -->
<!-- begin main -->
<div id="main"></div>
<!-- end main -->
<!-- begin footer -->
<div id="footer"></div>
<!-- end footer -->
</body>
</html>
I usually cut the portions of HTML and distributed them in different files so that I had something like this:
<html>
<head></head>
<body>
<!-- begin header -->
<?php require_once('header.php')?>
<!-- end header -->
<!-- begin main -->
<?php require_once('main.php')?>
<!-- end main -->
<!-- begin footer -->
<?php require_once('footer.php')?>
<!-- end footer -->
</body>
</html>
so that if I changed something in "header.php" was visualized in all the other templates that required the file, which is the correct way to do this in Yii?
thanks for your answers
......header here......
<?php echo $content; ?>
......footer here......
Read this first
Everything in Yii is in the layout file under views->layouts->main.php. This is where you would handle all of the changes that take affect throughout the entire site. For more complex sites you can use multiple layouts, column layouts etc.
If you decide to use one of the multiple column layouts then they still refer back to the main layout for the header, footer, etc.
Related
I have a form currently with a text area.
However, I want it to have rich-text formatting (bold, bullet points etc..) and for that, I have found Quill, which is amazing.
I'm not sure how to get the contents of div id=editor and save that to the Django function, because it won't be part of the post, will it?
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<!-- Create the editor container -->
<div id="editor">
<p>Hello World!</p>
<p>Some initial <strong>bold</strong> text</p>
<p><br></p>
</div>
<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
<!-- Initialize Quill editor -->
<script>
var quill = new Quill('#editor', {
theme: 'snow'
});
</script>
It is my first app with laravel. I tried too much to follow template rules to implement them in my app but actually I don't know did it in a correct way or not. Using the way I did, when I need to Redirect to a previous page it backs to / page and for example when I need to stay on the same page after submitting a form, I can not make the specific view or maybe I don't know how to use it. Do you think implementation is correct or not?
This is my master view:
<!DOCTYPE html>
<html lang="en" dir="rtl">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
<meta charset="utf-8">
<title>{{ $page_title }}</title>
{{ Html::style('css/styles.css') }}
{{ HTML::script('js/core/app.js') }}
</head>
<body>
<!-- Header -->
#include('header')
<!-- Header -->
<!-- Sidebar -->
#include('sidebar')
<!-- Sidebar -->
<!-- Main content -->
<div class="content-wrapper">
<div id="container"></div>`
</div>
<!-- /main content -->
</body>
</html>
This is my typicall included view:
#extends('Content_template')
#section('pageContents_js_files')
<script type="text/javascript" src="{{ asset("js/validate.min.js") }}"></script>
#stop
#section('pageContents')
<div></div>
#endsection
And this is a piece of html that is attached to all the typical views:
#yield('pageContents_style_files')
#yield('pageContents_js_files')
<!-- Page header -->
<div></div>
<!-- /page header -->
<!-- Content area -->
<div class="content">
<!-- Page Contents -->
#yield('pageContents')
<!-- /Page Contents -->
<!-- Footer -->
#include('footer')
<!-- Footer -->
</div>
<!-- /content area -->
I am using ajax to include views to master template when an <a> tag with include class is clicked:
$(document).ready(function() {
$(".include").click(function(event)
{
$.ajax({
url: 'include/' + $(this).attr('id'),
type: 'GET',
success: function (data) {
$data = $(data);
$('#container').html($data);
}
});
//}
});
});
And finally the route (all the views are stored in pages directory):
Route::get('include/{param}',function($param){
return view('pages.'.$param);
}
It's not necessary to use ajax. Just yield all js an css files to master blade, also yield other pages to master, then you can call them through routes.
I'm using CakePHP 2.2.4 and have similar layouts. For one page, however, the <head> content is the same, but essentially the whole body is different. What I have is a website with a navbar used from twitter's bootstrap. On this one page, the navbar is completely different. I know the quick fix would be to create a layout for just that page, but what if I come across another page I need to make with a different navbar? What would be the "proper" MVC way of doing this?
If every view will have a navbar of some kind, then you can just use CakePHP Elements to display the bar, you would put the element call in your one layout file and set a variable from the controller which you pass to the element to show a specific element...
echo $this->element('navbar', array(
"which_element" => "thisone"
));
In the above example, your navbar.ctp would have to contain all navbars and use a PHP Switch statement or something to work out which to display based on the $which_element...
Or better still, just call the element directly using the variable from the controller
$this->set('navbar', "thisone"); // this line is in your controller and sets the file name of your nav bar, minus the .ctp extension
echo $this->element($navbar); //this line is in your layout.ctp and renders elements/thisone.ctp, in the above example.
If some pages will have a nav bar but some will not, use View Blocks
$this->start('navbar');
echo $this->element($navbar);
$this->end();
I guess it depends how complex the differences are.
One way to go would be to have a common layout file
// in app/View/Common/layout.ctp
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Your header content -->
</head>
<body>
<div id="wrap">
<div class="navbar">
<?php echo $this->fetch('menu'); ?>
</div>
<div class="container">
<?php echo $this->fetch('content'); ?>
</div>
</div>
<div id="footer">
<?php echo $this->fetch('footer'); ?>
</div>
</body>
</html>
Have your layout file extending it
//app/View/Layouts/default.ctp
<?php
$this->extend('/Common/layout');
$this->assign('menu', $this->element('menu'));
echo $this->fetch('content');
$this->assign('footer', $this->element('footer'));
?>
I have the following simple Diazo rules file:
<rules
xmlns="http://namespaces.plone.org/diazo"
xmlns:css="http://namespaces.plone.org/diazo/css"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<theme href="theme/theme.html" />
<replace css:theme-children="#content" css:content-children=".content" />
</rules>
and theme:
<html>
<body>
<div id="content">
Lorem ipsum ...
</div>
</body>
</html>
The source I want to transform is:
<html>
<body>
<div class="content">
info
</div>
</body>
</html>
What I get is
... info ...
but I want to keep the HTML entities of the href attribute intact. How can I do this with Diazo?
Note numeric character references are not entity references so your title is a bit misleading (the answer for preserving or not entity references such as "& n b s p ; " is very different)
I don't know Diazo but in XSLT if you add
<xsl:output encoding="US-ASCII"/>
to your document then any non ascii characters will be output using numeric references.
However in your example they are in fact ascii characters that are quoted such as "." as "." There isn't any standard way in xslt 1 to do that (and there should never be any reason to do that if the document is going to be processed by a conforming html or xml system). Any such system will expand those references to their characters before processing starts. (Which is why XSLT can not preserve them: they have been removed by the xml parser before XSLT sees the input data.)
I am trying to learn Play 2.0 with scala but I dont think i quite understand how the template system for play 2.0 works. I have used play 1.2 before and i am sort of looking for an equivalent to the #{include 'views/blah.html' /}. I essentially want to create a navbar that is rendered on all the pages.
Essentially in main.scala.html i have
#(title: String)(navbar: Html)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="#routes.Assets.at("images/favicon.png")">
<script src="#routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
</head>
<header>
This is my header
</header>
<section class="navbar">#navbar</section>
<section class="content">#content</section>
<footer>
This is my footer
</footer>
and in my index.scala.html:
#navbar = {
<h1>Index</h1>
<ul>
<li>
<a href=#routes.Application.tasks>Tasks</a>
</li>
</ul>
}
#main("Home")(navbar){
content
}
in task.scala.html:
#(tasks: List[Task], taskForm: Form[String])
#import helper._
#main("Home") {
<h1>Index</h1>
<ul>
<li>
<a href=#routes.Application.tasks>Tasks</a>
</li>
</ul>
} {
task code
}
Now to include this navbar it seems i have to repeat this in every page this way i would have to hard code this navbar into every page. Is there a way to do this without without writing the whole navbar in every page?
I have also tried creating a navbar.scala.html file that contains
<h1>Index</h1>
<ul>
<li>
<a href=#routes.Application.tasks>Tasks</a>
</li>
</ul>
and saving under views/ then importing that using #import views.navbar but then i get an error stating 'navbar is not a member of views'. I am writing this in Eclipse Java EE IDE indigo if that helps.
Dont import it but just call it:
#navbar()
To include any other views template into another views template,
you simple call it using: #views.html.[location].[location].[location]()
Where [location] is just a break down of it's path.
for example:
#views.html.users.interface()
Be sure to put the "()" ie the brackets at the end of the statement if it does not take any parameters. Without the "()" you will get an error message like this:
"BaseScalaTemplate(play.api.templates...)"
If your template has parameters, be sure to include them when you call it, like this:
#views.html.users.interface( "name" )