2ちゃんねる ■掲示板に戻る■ 全部 1- 最新50    

■ このスレッドは過去ログ倉庫に格納されています

twigのincludeとimportは何が違うの?

1 :ハザマさんだナ:2024/07/10(水) 17:29:30.96 ID:RcrXi80IV
{% import 'custom_macros.twig' as macros %}
{# {% macro alert(type, message) %}<div class="alert alert-{{ type }}">{{ message }}</div>{% endmacro %}
{% macro link(url, text) %}<a href="{{ url }}">{{ text }}</a>{% endmacro %} #}
{{ macros.alert('success', '操作が成功しました') }}
{{ macros.link('https://example.com', 'Visit Example') }}
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ site_title }}</title><style>
body {font-family: Arial, sans-serif;line-height: 1.6;margin: 20px;}
header, nav, main, footer {margin-bottom: 20px;padding: 10px;
background-color: #f0f0f0;}
nav ul {list-style-type: none;padding: 0;}
nav ul li {display: inline;margin-right: 10px;}</style></head><body>
<header><h1>{{ site_title }}</h1><div class="container">
<div class="sidebar">{% include 'sidebar.html.twig' %}</div>
{# <h2>Sidebar</h2><ul><li><a href="/">Home</a></li><li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li><h2>test</h2></ul> #}
<div class="main-content">{% block content %}{% endblock %}</div></div>
{% block title %}About Us - My Site{% endblock %}
{% block content2 %}<h2>About Us</h2><p>Welcome to our site! Here's some information about us...</p>{% endblock %}
<p>{{ message }}</p></header><nav><ul>
<li><a href="#">Home</a></li><li><a href="#">About Us</a></li><li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li></ul></nav><main>
<section><h2>Welcome to {{ site_title }}</h2><p>{{ lorem_ipsum }}</p><p>Explore our services:</p>
<ul>{% for service in services %}<li>{{ service }}</li>{% endfor %}</ul></section>
</main><footer><p>© {{ current_year }} {{ site_title }}</p></footer></body></html>

2 :ハザマさんだナ:2024/07/10(水) 17:30:04.82 ID:RcrXi80IV
<?php
require_once 'vendor/autoload.php';$loader = new \Twig\Loader\FilesystemLoader(__DIR__);
$twig = new \Twig\Environment($loader);$siteTitle = 'Fake Site';
$loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
$services = ['Web Development', 'Graphic Design', 'SEO Optimization'];
$currentYear = date('Y');
class Router {protected $routes = [];public function get($route, $callback) {
$this->routes['GET'][$route] = $callback;}
public function dispatch() {$method = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];if (isset($this->routes[$method][$uri])) {
$callback = $this->routes[$method][$uri];call_user_func($callback);
} else {http_response_code(404);echo '404 Page Not Found';}}}$router = new Router();
$router->get('/', function () use ($twig) {$template = $twig->load('flame.twig');
echo $template->render(['site_title' => 'My Framework','message' => 'Welcome to my framework!',]);});
$router->dispatch();$template = $twig->load('test3.twig');
echo $template->render(['site_title' => $siteTitle,'lorem_ipsum' => $loremIpsum,
'services' => $services,'current_year' => $currentYear,]);
?>

3 :ニート ◆Ajwc/K4ZvEJb:2024/07/10(水) 17:36:35.03 ID:rVZyijpx/ ?2BP(0)
https://twiger.web.fc2.com/index.html
文字化けしてますよ

4 :ハザマさんだナ:2024/07/10(水) 18:24:15.96 ID:RcrXi80IV
要するにこういう事

include→文章をそのまま
htmlタグなどを代入する
import→変数などを取得する

<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>タヌキのフェイクゲーム</title><style>
body {font-family: Arial, sans-serif;text-align: center;background-color: #f0f0f0;}
.tanuki-img {max-width: 300px;margin: 20px auto;display: block;}
.message {background-color: #fff;padding: 10px;border: 1px solid #ccc;
border-radius: 5px;max-width: 80%;margin: 20px auto;}</style></head><body>
<h1>タヌキのフェイクゲーム</h1><img class="tanuki-img" src="tanuki.png" alt="タヌキの画像">
<div class="message">
{# (。♥‿♥。)(include){% macro cuteArt() %}(。♥‿♥。)(import){% endmacro %} #}
{% import 'kawaii_art.twig' as macros %}{% include 'kawaii_art.twig'%}
{{ macros.cuteArt() }}<p>{{ randomMessage }}</p></div></body></html>

<?php
require_once 'vendor/autoload.php';
$loader = new \Twig\Loader\FilesystemLoader(__DIR__);
$twig = new \Twig\Environment($loader);
$messages = ["今日はタヌキがおやつを食べています。","タヌキが夜中にお祭り騒ぎをしています。",
"タヌキが風呂に入っています。","タヌキがお手をしています。","タヌキがダンスをしています。",];
echo $twig->render('racoon.twig', ['randomMessage' => $messages,]);
?>

総レス数 4
6 KB
掲示板に戻る 全部 前100 次100 最新50
read.cgi ver.24052200