-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.html
49 lines (47 loc) · 15.3 KB
/
view.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Views | Pinatra</title>
<meta name="description" content="A PHP verison of Sinatra: a DSL for quickly creating web applications in PHP with minimal effort.">
<link rel="preload" href="/assets/css/0.styles.73742341.css" as="style"><link rel="preload" href="/assets/js/app.35633900.js" as="script"><link rel="preload" href="/assets/js/11.fbd446e0.js" as="script"><link rel="prefetch" href="/assets/js/4.dd1ee4a1.js"><link rel="prefetch" href="/assets/js/2.9834f0cc.js"><link rel="prefetch" href="/assets/js/3.2b86a6bc.js"><link rel="prefetch" href="/assets/js/5.ad87e3bc.js"><link rel="prefetch" href="/assets/js/6.7a49fda2.js"><link rel="prefetch" href="/assets/js/7.490e94d8.js"><link rel="prefetch" href="/assets/js/8.3b2a41b2.js"><link rel="prefetch" href="/assets/js/9.cfda04a6.js"><link rel="prefetch" href="/assets/js/10.20f55d86.js"><link rel="prefetch" href="/assets/js/12.7418dee3.js"><link rel="prefetch" href="/assets/js/13.111e6ced.js"><link rel="prefetch" href="/assets/js/14.78258348.js"><link rel="prefetch" href="/assets/js/15.73297d5e.js"><link rel="prefetch" href="/assets/js/16.63b4556e.js"><link rel="prefetch" href="/assets/js/17.c9391c34.js"><link rel="prefetch" href="/assets/js/18.584449ed.js"><link rel="prefetch" href="/assets/js/19.c8dececa.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.41d79f37.js">
<link rel="stylesheet" href="/assets/css/0.styles.73742341.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Pinatra</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://github.com/Pinatra/Pinatra" target="_blank" rel="noopener noreferrer" class="nav-link external">
Source
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/view.html" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/view.html" class="nav-link">简体中文</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="https://github.com/Pinatra/Pinatra" target="_blank" rel="noopener noreferrer" class="nav-link external">
Source
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/view.html" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/view.html" class="nav-link">简体中文</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>Guide</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/" class="sidebar-link">Introduction</a></li><li><a href="/install.html" class="sidebar-link">Installation</a></li><li><a href="/routing.html" class="sidebar-link">Routing</a></li><li><a href="/controller.html" class="sidebar-link">Controller</a></li><li><a href="/view.html" class="active sidebar-link">Views</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/view.html#preparation" class="sidebar-link">Preparation</a></li><li class="sidebar-sub-header"><a href="/view.html#creating-a-view-file" class="sidebar-link">Creating a view file</a></li><li class="sidebar-sub-header"><a href="/view.html#calling-a-view-and-passing-in-parameters" class="sidebar-link">Calling a view and passing in parameters</a></li><li class="sidebar-sub-header"><a href="/view.html#way-of-passing-parameters" class="sidebar-link">Way of passing parameters</a></li></ul></li><li><a href="/model.html" class="sidebar-link">Model</a></li><li><a href="/kernel.html" class="sidebar-link">Kernel details</a></li><li><a href="/benchmark.html" class="sidebar-link">Benchmark</a></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="views"><a href="#views" aria-hidden="true" class="header-anchor">#</a> Views</h1> <p>Views are a low-cost component of Pinatra with almost no performance penalty, using PHP itself as a template engine.</p> <h2 id="preparation"><a href="#preparation" aria-hidden="true" class="header-anchor">#</a> Preparation</h2> <p>Before using the view component, we need to create a new folder as the root of the view files and define a constant to pass this information.</p> <h3 id="new-folder"><a href="#new-folder" aria-hidden="true" class="header-anchor">#</a> New folder</h3> <p>Create a new folder <code>views</code> as the same level as <code>public</code>.</p> <h3 id="tell-pinatra"><a href="#tell-pinatra" aria-hidden="true" class="header-anchor">#</a> Tell Pinatra</h3> <p>Add the following line of code to <code>public/index.php</code>:</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token function">Define</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'VIEW_BASE_PATH'</span><span class="token punctuation">,</span> <span class="token function">dirname</span><span class="token punctuation">(</span><span class="token function">dirname</span><span class="token punctuation">(</span><span class="token constant">__FILE__</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'/views/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="creating-a-view-file"><a href="#creating-a-view-file" aria-hidden="true" class="header-anchor">#</a> Creating a view file</h2> <p>Create a <code>views/home.php</code> file with the following code:</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">echo</span> <span class="token variable">$title</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'<br>'</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$home_page</span><span class="token punctuation">;</span>
</span></code></pre></div><h2 id="calling-a-view-and-passing-in-parameters"><a href="#calling-a-view-and-passing-in-parameters" aria-hidden="true" class="header-anchor">#</a> Calling a view and passing in parameters</h2> <p>Whether in a closure or in a controller method, you can simply call the view with the following statement:</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token function">get</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'home'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withTitle</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'Pinatra'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">withHomePage</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'HomePage!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">class</span> <span class="token class-name">HomeController</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">home</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'home'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withTitle</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'Pinatra'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">withHomePage</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'HomePage!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Visit the right URL and you will get the following page:</p> <p><img src="/assets/img/5.8cab4f74.jpg" alt="view"></p> <div class="tip custom-block"><p class="custom-block-title">Tips</p> <p>view() receives a string as a file path that supports the use of multi-level subfolders, separated by <code>/</code> or <code>.</code>, such as <code>admin/home</code> or <code>admin.home</code>.</p></div> <h2 id="way-of-passing-parameters"><a href="#way-of-passing-parameters" aria-hidden="true" class="header-anchor">#</a> Way of passing parameters</h2> <h3 id="with"><a href="#with" aria-hidden="true" class="header-anchor">#</a> with()</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token function">view</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'home'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'title'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'Piantra'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// $title == 'Pinatra'</span>
</code></pre></div><h3 id="all-functions-starting-with-with"><a href="#all-functions-starting-with-with" aria-hidden="true" class="header-anchor">#</a> All functions starting with with</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token function">view</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'home'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withTitle</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'Piantra'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// $title == 'Pinatra'</span>
<span class="token function">view</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'home'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withtitle</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'Piantra'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// $title == 'Pinatra'</span>
<span class="token function">view</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'home'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withHomePage</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'Piantra'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// $home_page == 'Pinatra'</span>
</code></pre></div><blockquote><p>withSomeThing() will be translated from camelCase to snake_case when injected into a view.</p></blockquote> <h3 id="return"><a href="#return" aria-hidden="true" class="header-anchor">#</a> Return</h3> <p>The call to the view must use the <code>return view()</code> statement.</p></div> <div class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">9/6/2018, 5:42:56 PM</span></div></div> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/controller.html" class="prev">
Controller
</a></span> <span class="next"><a href="/model.html">
Model
</a>
→
</span></p></div> </div> <!----></div></div>
<script src="/assets/js/11.fbd446e0.js" defer></script><script src="/assets/js/app.35633900.js" defer></script>
</body>
</html>