How to open a view inside a view with CodeIgniter 4? - codeigniter-4

I am new with CodeIgniter 4. At CodeIgniter 3 we can use a view inside another view like this :
<head>
<?php this->load->view("containers/head"); ?>
</head>
But same is not working with CodeIgniter 4 and I couldn't find it. Any suggestions?
Have a great day!

Okay, for those who may search this, I used this and it worked.
<head>
<?php echo view("containers/head"); ?>
</head>

I recommend that you read the documentation.
View Layouts in CI4
My view layout template layout.php
[...]
<head>
<?= $this->renderSection('head')?>
</head>
[...]
My view
<?= $this->extend('layout') ?>
<?= $this->section('head') ?>
<!-- Code in block <head> -->
<?= $this->endSection('') ?>
It may also be helpful: View Parser in CI4

Related

How much does include statements hinder performance?

I am currently writing a node server, using express and ejs for the tempting engine. Some of my .ejs files have 7 - 8 include statements for partials nested inside of them. I was wondering if this is resource intensive, or if it will be fine.
EJS maintainer here.
First off, if you're reading the same file many times in a row, the filesystem will cache it on that level.
However, if you want maximum performance, set cache: true in the options. (docs) You must be using renderFile() or passing in the filename to get this to work. If you're using Express, cache: true is set automatically when NODE_ENV is set to production.
Keep in mind that this assumes that your files never change. i.e. if you change a file, EJS will just use the old cached version until you restart the server process.
I've tested the include load time for 29 partials:
<!DOCTYPE html>
<html lang="en">
<? console.time('ejsIncludeTime') ?>
<?include ../partials/head ?>
<body>
<? include ../partials/header ?>
<? include ../partials/a ?>
<? include ../partials/b ?>
<? include ../partials/c ?>
<? include ../partials/d ?>
<? include ../partials/e ?>
<? include ../partials/f ?>
<? include ../partials/g ?>
<? include ../partials/h ?>
<? include ../partials/i ?>
<? include ../partials/j ?>
<? include ../partials/k ?>
<? include ../partials/l ?>
<? include ../partials/m ?>
<? include ../partials/n ?>
<? include ../partials/o ?>
<? include ../partials/p ?>
<? include ../partials/q ?>
<? include ../partials/r ?>
<? include ../partials/s ?>
<? include ../partials/t ?>
<? include ../partials/u ?>
<? include ../partials/v ?>
<? include ../partials/w ?>
<? include ../partials/x ?>
<? include ../partials/y ?>
<? include ../partials/z ?>
<? include ../partials/footer ?>
<? console.timeEnd('ejsIncludeTime') ?>
</body>
</html>
Each partial from a-z contains 120 or more lines of html. Here's how it goes:
First hit after the server restart ejsIncludeTime: 0.175ms
Consequent hits ejsIncludeTime: 0.078ms, 0.068ms, 0.058ms, 0.067ms, 0.077ms
Restart the server again and hit ejsIncludeTime: 0.157ms
Consequent hits 0.168ms(hit after a few minutes gap), 0.044ms, 0.052ms
It therefore appears that there's caching involved on some level. The first hit always takes 0.150+ms of include time. Consequent hits are always much less time consuming except for an occasional peak like 0.168ms (can't really explain why. Maybe cache miss).

Sails set css in specific page

I'm using the sails.js framework
I want to know if there is a way for a view to use only one css file, not the other ones being declared in the layout?
That is, ignore the other css definitions and use only one.
Is that possible?
Yes. Inside of the method that renders your view, return a value that references the stylesheet you want to include.
return res.view('someTemplate',{
myStylesheet: 'link/to/your/stylesheet.css'
})
Then, edit your layout.ejs file to include the stylesheet if our new value is present.
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
...
<% if(myStylesheet) { %>
<link rel="stylesheet" href="<%= myStylesheet %>">
<% } %>
<!--STYLES-->
<link rel="stylesheet" href="/some/other/stylesheet.css">
<!--STYLES END-->
...
...
...
The EJS template pre-processor will now only include the stylesheet you specify if a value is provided.

Create article preview in ModX?

How can I create preview of my articles(which are in the resource "articles") using their [[*description]] and [[*introtext]] in the right sidebar on another resources page, called "services"(this page's (template) code:
<html>
<head>
<title>[[++site_name]] - [[*pagetitle]]</title>
<base href="[[++site_url]]" />
</head>
<body>
[[*content]]
</body>
</html>
)
I created new tpl, called it "article_story" with this code:
<p><a href="[[++site_url]][[~[[+id]]]]">[[+pagetitle]]<br>
<img alt="[[+pagetitle]]" src="[[+tv.image:phpthumbof=`w=300`]]"></a><br>
[[+introtext]]<br>
</p>
I've tried to use this code:
[[!getResources? &parents=`[[*id]]` &tpl=`article_story` &limit=`3` &sortby=`{"publishedon":"ASC","createdon":"DESC"}`]]
But it doesn't work. Any thoughts?
By default the getResources Extra doesn't include unpublished Resources. You need to add &showUnpublished to the snippet call:
[[!getResources?
&parents=`[[*id]]`
&tpl=`article_story`
&showUnpublished=`1`
&limit=`3`
&sortby=`{"publishedon":"ASC","createdon":"DESC"}`
]]

Template engine for express 4 supporting Layouts

I'm looking for alternatives to Jade templates in express 4.x because I really don't like Jade's syntax. I'm tending towards EJS, because it's basically just HTML on steroids.
However, one really nice feature of Jade templates is the ability to use layouts. I've found https://www.npmjs.org/package/express-ejs-layouts, but it seems to be made for express 3 and its build is failing :/.
I also found https://www.npmjs.org/package/ejs-mate which is made for express 4.x but it only seems to support a single content block (body).
I would like to have something like this:
layout.something:
<html>
<head>
<% block styles %>
<% block scripts %>
</head>
<body>
<% block body %>
</body>
</html>
index.html:
uses layout "layout.somehing"
scripts:
<script src="my_custom_script.js"></script>
styles:
<link rel="stylesheet ...></link>
body:
<h1>This is my body!</h1>
So that this yields:
<html>
<head>
<link rel="stylesheet ...></link>
<script src="my_custom_script.js"></script>
</head>
<body>
<h1>This is my body!</h1>
</body>
</html>
Does anyone know an engine that is capable of that besides Jade?
You can try express-handlebars, it supports layout and partial views.

How to modify the HTML tag in Drupal 6?

I am new to Drupal and I need to add an attribute to the HTML tag, but I cannot find the way. Thank you for the help.
This probably depends in part on your theming mechanism. However, the doctype and html tags are likely hard-coded in page.tpl.php or similar (located in sites/all/themes/themename). The exact file will depend on your theme.
For example, I have a site with the following in page.tpl.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language; ?>" lang="<?php print $language->language; ?>" dir="<?php print $language->dir; ?>">
As you can see, there is some information being injected by PHP. This would be the place for you to make your change.

Resources