twigのimportとincludeはどこが違うの?
1 :デフォルトの名無しさん :2024/07/10(水) 18:51:55.66 ID:G1nCEzbNC {% 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!</p>{% endblock %} <p>{{ message }}</p></header><nav></nav><main> </main><footer><p>© {{ current_year }} {{ site_title }}</p></footer></body></html>
2 :デフォルトの名無しさん :2024/07/10(水) 18:52:29.61 ID:G1nCEzbNC <?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 :デフォルトの名無しさん :2024/07/10(水) 18:54:44.07 ID:G1nCEzbNC import→変数などを引き継ぎ代入する include→文章をそのまま代入シたり htmlのタグなどを使う時に <!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 :デフォルトの名無しさん :2024/07/10(水) 19:31:52.90 ID:G1nCEzbNC ******twig_base****** <!DOCTYPE html><html lang="ja"><head> <title>{% block title %}{% endblock %}</title> <link rel="stylesheet" href="styles.css"></head><body><header> <nav><ul><li><a href="/">ホーム</a></li><li><a href="/products">商品一覧</a></li> <li><a href="/cart">カート</a></li></ul></nav></header> <main>{% block content %}{# ページ固有のコンテンツをここに書く #}{% endblock %}</main> <footer>© {{ 'now'|date('Y') }} ECサイト. All Rights Reserved.</footer></body></html> ******twig_main****** {% extends 'base_main.twig' %}{% block title %}商品一覧 - ECサイト{% endblock %} {% block content %}<h1>商品一覧</h1><div class="product-list"> {% for product in products %}<div class="product"><h2>{{ product.name }}</h2> <p>{{ product.description }}</p><p>価格: {{ product.price }}円</p> <a href="/product/{{ product.id }}">詳細を見る</a> <form action="/cart/add" method="POST"><input type="hidden" name="product_id" value="{{ product.id }}"> <button type="submit">カートに入れる</button></form></div>{% endfor %}</div>{% endblock %}
5 :デフォルトの名無しさん :2024/07/10(水) 19:32:27.83 ID:G1nCEzbNC ******php****** <?php require_once 'vendor/autoload.php';$loader = new \Twig\Loader\FilesystemLoader(__DIR__); $twig = new \Twig\Environment($loader); $products = [['name' => '犬', 'description' => 'dog.jpg', 'price' => '1000', 'decorations' => ['Daiya', 'Asta','Star']], ['name' => '猫', 'description' => 'cat.jpg', 'price' => '1200', 'decorations' => ['Daiya', 'Asta','Star']], ['name' => 'ウサギ', 'description' => 'rabbit.jpg', 'price' => '2000', 'decorations' => ['Daiya', 'Asta','Star']],]; echo $twig->render('layout.twig', ['products' => $products,]); ?>
6 :デフォルトの名無しさん :2024/07/10(水) 20:37:35.04 ID:G1nCEzbNC {% set max_attempts = 3 %}<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"> <title>Escape Game</title></head><body><h1>Escape Game</h1><p>Guess the secret number between 1 and 10!</p> {# {{secret_number}} #}{% block content %}{% set attempts = attempts + 1 %}{% if attempts > max_attempts %} <p>Sorry, you failed to guess the number within {{ max_attempts }} attempts.</p> <p>The secret number was {{ secret_number }}.</p>{% endif %} <form action="?attempts={{attempts}}&secret_number={{secret_number}}" method="post"> <label for="guess">Enter your guess (1-10):</label> <input type="number" id="guess" name="guess" min="1" max="10" required> <button type="submit">Guess</button></form>{% if guess == secret_number %} <p>Congratulations! You guessed the secret number {{ secret_number }}!</p>{% else %} <p>Sorry, that's not the correct number. Try again!</p>{% endif %} {% endblock %}</body></html> <?php require_once 'vendor/autoload.php'; $loader = new \Twig\Loader\FilesystemLoader(__DIR__); $twig = new \Twig\Environment($loader); if ($_SERVER['REQUEST_METHOD'] === 'POST') {$guess = $_POST['guess'];}else{$guess = 0;} if (isset($_GET['attempts'])) {$attempts = $_GET['attempts'];}else{$attempts = 0;} if (isset($_GET['secret_number'])) {$secret_number = $_GET['secret_number']; }else{$secret_number = rand(1,10);} echo $twig->render('escape_while.twig', ['guess' => $guess,'attempts' => $attempts,'secret_number' => $secret_number]); ?>
10 KB
新着レスの表示
掲示板に戻る
全部
前100
次100
最新50
read.cgi ver.24052200
本文 スレッドタイトル 投稿者