{"id":48552,"date":"2024-12-02T14:58:48","date_gmt":"2024-12-02T06:58:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48552\/"},"modified":"2024-12-02T14:58:48","modified_gmt":"2024-12-02T06:58:48","slug":"%e4%bd%bf%e7%94%a8-twig-%e9%80%9a%e8%bf%87-php-%e6%b8%b2%e6%9f%93-markdown","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48552\/","title":{"rendered":"\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730077791671ee45fa17fb.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown\u63d2\u56fe\" alt=\"\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown\u63d2\u56fe\" \/><\/p>\n<p><strong>twig<\/strong> \u662f\u4f7f\u7528 symfony \u5f00\u53d1 web \u5e94\u7528\u7a0b\u5e8f\u65f6\u6e32\u67d3 html \u7684\u9996\u9009\u6a21\u677f\u5f15\u64ce\u3002<br \/> \u7136\u800c\uff0ctwig \u7684\u7075\u6d3b\u6027\u4e0d\u4ec5\u4ec5\u9650\u4e8e\u751f\u6210 html \u9875\u9762\u3002\u5b83\u53ef\u4ee5\u6210\u4e3a\u8de8\u591a\u4e2a\u6e20\u9053\u4ea4\u4ed8\u5185\u5bb9\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u4f8b\u5982<strong>\u751f\u6210 markdown \u6587\u4ef6<\/strong>\u3001json \u8f93\u51fa\uff0c\u751a\u81f3\u7eaf\u6587\u672c\uff0c\u6240\u6709\u8fd9\u4e9b\u90fd\u6765\u81ea\u540c\u4e00\u7ec4\u5185\u5bb9\u3002<\/p>\n<p>\u8fd9\u79cd\u9002\u5e94\u6027\u4f7f\u60a8\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u9891\u9053\u521b\u5efa\u5185\u5bb9\u3002<\/p>\n<blockquote>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 twig \u751f\u6210 html\u3001markdown\u3001json\u3001\u6587\u672c\u7b49<\/p>\n<\/blockquote>\n<h2> \u7528\u4f8b\uff1a\u4f7f\u7528 symfony \u83b7\u53d6\u5e76\u6e32\u67d3\u914d\u65b9\u4e3a markdown <\/h2>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 symfony \u7684 http \u5ba2\u6237\u7aef\u4ece\u5916\u90e8 api (https:\/\/dummyjson.com\/recipes\/1) \u83b7\u53d6\u914d\u65b9\uff0c\u5e76\u4f7f\u7528 twig \u5c06\u5176\u5448\u73b0\u4e3a markdown \u6587\u6863\u3002<br \/> \u8fd9\u79cd\u65b9\u6cd5\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408 symfony \u5f3a\u5927\u7684\u8f6f\u4ef6\u5305\uff08\u4f8b\u5982\u7528\u4e8e\u5916\u90e8\u6570\u636e\u68c0\u7d22\u7684 symfony http \u5ba2\u6237\u7aef\u548c\u7528\u4e8e\u6e32\u67d3\u89c6\u56fe\u7684 twig\uff09\u6765\u8de8\u591a\u4e2a\u6e20\u9053\u4ea4\u4ed8\u5185\u5bb9\uff0c\u4f8b\u5982\u547d\u4ee4\u884c\u5de5\u5177\u4e2d\u7684 markdown \u62a5\u544a\u3002<\/p>\n<p>\u6211\u4eec\u8981\u6784\u5efa\u7684\u547d\u4ee4\/\u811a\u672c\u4ece api \u4e2d\u83b7\u53d6\u98df\u8c31\u6570\u636e\uff08\u6807\u9898\u3001\u63cf\u8ff0\u3001\u6210\u5206\u548c\u8bf4\u660e\uff09\uff0c\u5bf9\u5176\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u4f7f\u7528 twig \u4ee5\u7ed3\u6784\u5316 markdown \u683c\u5f0f\u8f93\u51fa\u5185\u5bb9\u3002\u8fd9\u4e2a\u5b9e\u9645\u7528\u4f8b\u8bf4\u660e\u4e86\u5982\u4f55\u5728 web \u6a21\u677f\u4e4b\u5916\u4f7f\u7528 twig\uff0c\u4f7f\u5176\u80fd\u591f\u751f\u6210\u5404\u79cd\u683c\u5f0f\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\uff1a<\/p>\n<ul>\n<li> <strong>symfony console<\/strong> \u7528\u4e8e\u6784\u5efa\u547d\u4ee4\u884c\u5de5\u5177\u7684\u7ec4\u4ef6 https:\/\/symfony.com\/doc\/current\/components\/console.html<\/li>\n<li> <strong>symfony http \u5ba2\u6237\u7aef<\/strong> https:\/\/symfony.com\/doc\/current\/http_client.html \u7528\u4e8e\u83b7\u53d6\u548c\u4f7f\u7528 http api<\/li>\n<li>symfony <strong>twig<\/strong> https:\/\/twig.symfony.com\/doc\/3.x\/intro.html#installation \u7528\u4e8e\u6e32\u67d3\u6570\u636e<\/li>\n<li>\u83dc\u8c31<strong>api<\/strong>\uff1a\u6211\u4eec\u5c06\u9009\u62e9\u4e00\u4e2a\u201c\u865a\u62df\u201d\u83dc\u8c31 https:\/\/dummyjson.com\/recipes\/1 \u6765\u5c55\u793a\u548c\u6f14\u793a\u68c0\u7d22\u7ed3\u6784\u5316 json \u5e76\u5c06\u5176\u8f6c\u6362\u4e3a markdown \u6587\u4ef6\u7684\u8fc7\u7a0b.<\/li>\n<\/ul>\n<h2> \u5b89\u88c5\u5fc5\u8981\u7684 symfony \u7ec4\u4ef6 <\/h2>\n<p>\u786e\u4fdd\u60a8\u5df2\u5b89\u88c5 http \u8bf7\u6c42\u548c\u521b\u5efa\u547d\u4ee4\u6240\u9700\u7684\u7ec4\u4ef6\uff1a<\/p>\n<pre>composer require symfony\/http-client symfony\/console twig\/twig\n<\/pre>\n<h2> \u521b\u5efa symfony \u547d\u4ee4 <\/h2>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 symfony \u547d\u4ee4\u3002<\/p>\n<blockquote>\n<p>\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u6709\u5173\u5982\u4f55\u4f7f\u7528 symfony command \u7ec4\u4ef6\u521b\u5efa\u547d\u4ee4\u884c\u5de5\u5177\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u6211\u5199\u4e86\u4e00\u7bc7\u5173\u4e8e\u6b64\u7684\u5177\u4f53\u6587\u7ae0\uff1ahttps:\/\/dev.to\/robertobutti\/building-a-command-line-tool -with-php-and-symfony-console-4n6g<\/p>\n<\/blockquote>\n<p>\u547d\u4ee4\u7c7b\uff0c\u901a\u5e38\u8fdb\u5165 src\/commands \u76ee\u5f55\u3002<\/p>\n<pre># create a new empty directory\nmkdir -p src\/commands\n# create a new empty file\ntouch src\/commands\/fetchrecipecommand.php\n<\/pre>\n<p>\u547d\u4ee4\u7c7b\uff08\u4f8b\u5982\uff0csrc\/commands\/fetchrecipecommand.php\uff09\uff1a<\/p>\n<pre>&lt;?php\n\nnamespace myexample\\commands;\n\nuse symfony\\component\\console\\command\\command;\nuse symfony\\component\\console\\input\\inputinterface;\nuse symfony\\component\\console\\output\\outputinterface;\nuse symfony\\component\\httpclient\\httpclient;\n\n\nclass fetchrecipecommand extends command\n{\n\n    private $twig;\n\n    public function __construct()\n    {\n        $this\n            -&gt;setname('recipe')\n            -&gt;setdescription('prints a recipe in markdown')\n            -&gt;sethelp('this command prints a simple recipe in markdown.');\n        \/\/ step 1: loading the twig environment\n        $loader = new \\twig\\loader\\filesystemloader(__dir__ . '\/..\/resources\/views');\n        $twig = new \\twig\\environment(\n            $loader,\n            \/\/ optional: enable caching for better performance\n            \/*[\n                'cache' =&gt; __dir__ . '\/..\/..\/cache',\n            ]*\/\n        );\n\n        $this-&gt;twig = $twig;\n        parent::__construct();\n    }\n\n    protected function configure()\n    {\n        $this-&gt;setdescription('fetches a recipe from an api and renders it as markdown');\n    }\n\n    protected function execute(inputinterface $input, outputinterface $output): int\n    {\n        \/\/ step 2: initialize the http client and fetch the recipe\n        $client = httpclient::create();\n        $response = $client-&gt;request('get', 'https:\/\/dummyjson.com\/recipes\/1');\n\n        \/\/ step 3: obtain the array of info\n        $recipedata = $response-&gt;toarray();\n\n        \/\/ step 4: render the template using twig, returning a string\n        $markdownoutput = $this-&gt;twig-&gt;render('recipe.md.twig', $recipedata);\n\n        \/\/ step 5: output the generated markdown\n        $output-&gt;writeln($markdownoutput);\n\n        return command::success;\n    }\n}\n\n<\/pre>\n<p>\u4ee5\u4e0b\u662f fetchrecipecommand \u793a\u4f8b\u4e2d\u6bcf\u4e2a\u6b65\u9aa4\u7684\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<h3> \u9010\u6b65\u7ec6\u5206 <\/h3>\n<h4> \u7b2c1\u6b65\uff1a\u52a0\u8f7dtwig\u73af\u5883 <\/h4>\n<p>\u8981\u5728\u5178\u578b\u7684 web \u4e0a\u4e0b\u6587\u4e4b\u5916\u4f7f\u7528 twig\uff0c\u4f8b\u5982\u5728 php \u547d\u4ee4\u884c\u5de5\u5177\u4e2d\uff0c\u9996\u5148\u9700\u8981\u901a\u8fc7 twigenvironment \u7c7b\u624b\u52a8\u521d\u59cb\u5316\u5b83\u3002\u4ee5\u4e0b\u662f\u63a7\u5236\u53f0\u547d\u4ee4\u4e2d twig \u8bbe\u7f6e\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>$loader = new \\twig\\loader\\filesystemloader(__dir__ . '\/..\/resources\/views');\n$twig = new \\twig\\environment(\n    $loader,\n    \/\/ optional: enable caching for better performance\n    \/*[\n        'cache' =&gt; __dir__ . '\/..\/..\/cache',\n    ]*\/\n);\n$this-&gt;twig = $twig;\n\n<\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>twig \u73af\u5883\u662f\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a filesystemloader \u6765\u521d\u59cb\u5316\u7684\uff0c\u8be5 filesystemloader \u544a\u8bc9 twig \u5728\u54ea\u91cc\u53ef\u4ee5\u627e\u5230\u6a21\u677f\u3002\u672c\u4f8b\u6307\u5411\u5b58\u50a8 twig \u6a21\u677f\u7684 src\/resources\/views \u6587\u4ef6\u5939\u3002<\/li>\n<li>\u7136\u540e\uff0c$twig \u73af\u5883\u88ab\u5b9e\u4f8b\u5316\u5e76\u8d1f\u8d23\u6e32\u67d3\u6a21\u677f\u3002\u53ef\u4ee5\u542f\u7528\u53ef\u9009\u7f13\u5b58\uff0c\u901a\u8fc7\u5b58\u50a8\u9884\u7f16\u8bd1\u6a21\u677f\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li>\u6700\u540e\u5c06\u521d\u59cb\u5316\u7684$twig\u73af\u5883\u8d4b\u503c\u7ed9$this-&gt;twig\u4ee5\u4f9b\u540e\u7eed\u4f7f\u7528\uff08\u5728execute()\u65b9\u6cd5\u4e2d\uff09\u3002<\/li>\n<\/ul>\n<h4> \u7b2c 2 \u6b65\uff1a\u521d\u59cb\u5316 http \u5ba2\u6237\u7aef\u5e76\u83b7\u53d6\u914d\u65b9 <\/h4>\n<pre>$client = httpclient::create();\n$response = $client-&gt;request('get', 'https:\/\/dummyjson.com\/recipes\/1');\n<\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>symfony http \u5ba2\u6237\u7aef\u662f\u4f7f\u7528 httpclient::create() \u65b9\u6cd5\u521b\u5efa\u7684\uff0c\u8be5\u65b9\u6cd5\u5141\u8bb8\u547d\u4ee4\u6267\u884c http \u8bf7\u6c42\u3002<\/li>\n<li>\u8981\u83b7\u53d6\u83dc\u8c31\uff0crequest() \u65b9\u6cd5\u4f1a\u5bf9\u6307\u5b9a\u7684 url (https:\/\/dummyjson.com\/recipes\/1) \u6267\u884c get \u8bf7\u6c42\u3002<\/li>\n<li>api \u8fd4\u56de\u5b58\u50a8\u5728 $response \u53d8\u91cf\u4e2d\u7684 json \u54cd\u5e94\u3002<\/li>\n<\/ul>\n<h4> \u7b2c\u4e09\u6b65\uff1a\u83b7\u53d6\u4fe1\u606f\u6570\u7ec4 <\/h4>\n<pre>$recipedata = $response-&gt;toarray();\n<\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>toarray() \u65b9\u6cd5\u5c06 api \u7684 json \u54cd\u5e94\u8f6c\u6362\u4e3a php \u6570\u7ec4\u3002\u8be5\u6570\u7ec4\u5305\u542b\u98df\u8c31\u7684\u6570\u636e\uff08\u4f8b\u5982\u540d\u79f0\u3001\u6210\u5206\u3001\u8bf4\u660e\uff09\uff0c\u8fd9\u4e9b\u6570\u636e\u5c06\u7528\u4e8e\u5728\u4e0b\u4e00\u6b65\u4e2d\u586b\u5145 twig \u6a21\u677f\u3002<\/li>\n<\/ul>\n<h4> \u7b2c\u56db\u6b65\uff1a\u4f7f\u7528twig\u6e32\u67d3\u6a21\u677f <\/h4>\n<pre>$markdownoutput = $this-&gt;twig-&gt;render('recipe.md.twig', $recipedata);\n<\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>twig \u73af\u5883\u7684 render() \u65b9\u6cd5\u751f\u6210 markdown \u8f93\u51fa\u3002\u5b83\u4ece src\/resources\/views \u6587\u4ef6\u5939\u52a0\u8f7d\u6a21\u677f (recipe.md.twig)\u3002<\/li>\n<li>\u4ece api \u83b7\u53d6\u7684\u83dc\u8c31\u6570\u636e ($recipedata) \u88ab\u4f20\u9012\u5230\u6a21\u677f\u4e2d\uff0c\u5b83\u5c06\u66ff\u6362\u83dc\u8c31\u540d\u79f0\u3001\u6210\u5206\u548c\u8bf4\u660e\u7b49\u5360\u4f4d\u7b26\uff0c\u521b\u5efa\u4e00\u4e2a\u5b8c\u5168\u683c\u5f0f\u5316\u7684 markdown \u6587\u4ef6\u3002<\/li>\n<li>\u6e32\u67d3\u540e\u7684 markdown \u5185\u5bb9\uff08render() \u65b9\u6cd5\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\uff09\u5b58\u50a8\u5728 $markdownoutput \u53d8\u91cf\u4e2d\u3002<\/li>\n<\/ul>\n<h4> \u7b2c 5 \u6b65\uff1a\u4e3a\u751f\u6210\u7684 markdown \u751f\u6210\u8f93\u51fa <\/h4>\n<pre>$output-&gt;writeln($markdownoutput);\n<\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>\u6700\u7ec8\u7684 markdown \u5185\u5bb9\u4f7f\u7528 $output-&gt;writeln() \u65b9\u6cd5\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u5141\u8bb8\u7528\u6237\u4ee5 markdown \u683c\u5f0f\u67e5\u770b\u683c\u5f0f\u5316\u7684\u83dc\u8c31\uff08\u6700\u7ec8\uff0c\u60a8\u53ef\u4ee5\u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230 shell \u4e2d\u7684\u6587\u4ef6\u4e2d\uff09\u3002<\/li>\n<\/ul>\n<h2> \u521b\u5efa\u542f\u52a8\u6587\u4ef6 <\/h2>\n<p>\u60a8\u5fc5\u987b\u521b\u5efa\u4e00\u4e2a\u542f\u52a8\u6587\u4ef6\u4ee5\u5141\u8bb8\u7528\u6237\u76f4\u63a5\u4ece shell \u542f\u52a8\u60a8\u7684 symfony \u547d\u4ee4\u3002 \uff08\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4e0d\u662f\u521b\u5efa symfony \u5e94\u7528\u7a0b\u5e8f\uff1b\u6211\u4eec\u6b63\u5728\u4f7f\u7528 symfony \u5305\u6784\u5efa php \u811a\u672c\u3002\uff09<br \/> \u5728\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u60a8\u6709composer.json\u6587\u4ef6\u548csrc\u76ee\u5f55\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2amy-app\u6587\u4ef6\u3002<\/p>\n<pre>#!\/usr\/bin\/env php\n&lt;?php\n\nuse myexample\\commands\\fetchrecipecommand;\nuse symfony\\component\\console\\application;\n\nif (file_exists(__dir__ . '\/..\/..\/autoload.php')) {\n    require __dir__ . '\/..\/..\/autoload.php';\n} else {\n    require __dir__ . '\/vendor\/autoload.php';\n}\n\n\/**\n * start the console application.\n *\/\n$app = new application('recipe to markdown', '1.0.0');\n$app-&gt;setdefaultcommand(\"recipe\");\n\n$app-&gt;add(new fetchrecipecommand());\n\n\n$app-&gt;run();\n\n<\/pre>\n<p>\u8981\u6b63\u786e\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u548c\u7c7b\uff0c\u8bf7\u52a1\u5fc5\u5728composer.json \u6587\u4ef6\u4e2d\u8bbe\u7f6e\u81ea\u52a8\u52a0\u8f7d\u90e8\u5206\uff1a<\/p>\n<pre>{\n    \"require\": {\n        \"symfony\/http-client\": \"^7.1\",\n        \"symfony\/console\": \"^7.1\",\n        \"twig\/twig\": \"^3.14\"\n    },\n    \"autoload\": {\n        \"psr-4\": {\n            \"myexample\\\\\": \"src\/\"\n        }\n    }\n}\n\n<\/pre>\n<p>\u5982\u679c\u60a8\u66f4\u6539\u81ea\u52a8\u52a0\u8f7d\u90e8\u5206\uff0c\u6211\u5efa\u8bae\u8f6c\u50a8\u81ea\u52a8\u52a0\u8f7d\u6587\u4ef6\uff1a<\/p>\n<pre>composer dump-autoload\n<\/pre>\n<p>\u73b0\u5728\uff0c\u60a8\u5fc5\u987b\u521b\u5efa twig \u6a21\u677f\/\u89c6\u56fe\u6765\u5448\u73b0 api \u68c0\u7d22\u5230\u7684\u6570\u636e\u3002<\/p>\n<h2> \u4e3a\u83dc\u8c31\u521b\u5efa\u4e00\u4e2a twig \u6a21\u677f\/\u89c6\u56fe <\/h2>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a twig \u6a21\u677f\/\u89c6\u56fe\u4ee5\u4ee5 markdown \u683c\u5f0f\u5448\u73b0\u914d\u65b9\u3002<br \/> \u8be5\u6a21\u677f\u5e94\u4f4d\u4e8e\u89c6\u56fe\u76ee\u5f55\u4e2d\uff08\u4f8b\u5982 src\/resources\/view\/recipe.md.twig\uff09\u3002<\/p>\n<pre># recipe: {{ name }}\n\n- preparation time: {{ preptimeminutes }} minutes\n- cooking time {{ cooktimeminutes }} minutes\n- difficulty level: {{ difficulty }}\n- cuisine: {{ cuisine }}\n- servings {{ servings }} people, with {{ caloriesperserving }} calories per person\n\n## ingredients:\n\n{% for ingredient in ingredients %}\n- {{ ingredient }}\n{% endfor %}\n\n## instructions:\n\n{% for instruction in instructions %}\n- {{ instruction }}\n{% endfor %}\n\nenjoy!\n<\/pre>\n<p>\u6b64 twig \u89c6\u56fe\u6587\u4ef6\u5c06\u4ee5 markdown \u683c\u5f0f\u5448\u73b0\u83dc\u8c31\uff0c\u5176\u4e2d\u5305\u542b\u83dc\u8c31\u540d\u79f0\u3001\u6210\u5206\u548c\u8bf4\u660e\u90e8\u5206\u3002<\/p>\n<h2> \u8fd0\u884c\u547d\u4ee4 <\/h2>\n<p>\u8981\u6267\u884c\u8be5\u547d\u4ee4\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php my-app\n<\/pre>\n<p>\u5982\u679c\u60a8\u6709\u591a\u4e2a\u547d\u4ee4\uff0c\u60a8\u53ef\u4ee5\u542f\u52a8\uff1a<\/p>\n<pre>php m-app recipe\n<\/pre>\n<p>\u8981\u67e5\u770b\u6240\u6709\u53ef\u7528\u7684\u547d\u4ee4\uff1a<\/p>\n<pre>php my-app list\n<\/pre>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 symfony \u7684 http \u5ba2\u6237\u7aef\u8f7b\u677e\u5730\u4ece\u5916\u90e8 api \u68c0\u7d22\u6570\u636e\u3001\u5904\u7406\u54cd\u5e94\u5e76\u4f7f\u7528 twig \u4ee5\u7ed3\u6784\u5316\u683c\u5f0f\u5448\u73b0\u8f93\u51fa\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u8be5\u547d\u4ee4\u5c06\u914d\u65b9\u8f93\u51fa\u4e3a markdown\uff0c\u4f46\u6b64\u6280\u672f\u53ef\u4ee5\u9002\u7528\u4e8e\u60a8\u9700\u8981\u5904\u7406\u7684\u4efb\u4f55\u5176\u4ed6\u5185\u5bb9\u6216\u6570\u636e\u7c7b\u578b\u3002<br \/> \u4eab\u53d7\u4f60\u7684\u98df\u8c31\uff01<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown \u6536\u85cf \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4f7f\u7528 Twig \u901a\u8fc7 PHP \u6e32\u67d3 Markdown\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 twig \u662f\u4f7f\u7528 symfony \u5f00\u53d1 web \u5e94\u7528\u7a0b\u5e8f\u65f6\u6e32\u67d3 html \u7684\u9996\u9009\u6a21\u677f\u5f15\u64ce\u3002 \u7136\u800c\uff0ctwig \u7684\u7075\u6d3b\u6027\u4e0d\u4ec5\u4ec5\u9650\u4e8e\u751f\u6210 html \u9875\u9762\u3002\u5b83\u53ef\u4ee5\u6210\u4e3a\u8de8\u591a\u4e2a\u6e20\u9053\u4ea4\u4ed8\u5185\u5bb9\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u4f8b\u5982\u751f\u6210 markdown \u6587\u4ef6\u3001json \u8f93\u51fa\uff0c\u751a\u81f3\u7eaf\u6587\u672c\uff0c\u6240\u6709\u8fd9\u4e9b\u90fd\u6765\u81ea\u540c\u4e00\u7ec4\u5185\u5bb9\u3002 \u8fd9\u79cd\u9002\u5e94\u6027\u4f7f\u60a8\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u9891\u9053\u521b\u5efa\u5185\u5bb9\u3002 \u60a8\u53ef\u4ee5\u4f7f\u7528 twig \u751f\u6210 html\u3001markdown\u3001json\u3001\u6587\u672c\u7b49 \u7528\u4f8b\uff1a\u4f7f\u7528 symfony \u83b7\u53d6\u5e76\u6e32\u67d3\u914d\u65b9\u4e3a markdown \u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 symfony \u7684 http \u5ba2\u6237\u7aef\u4ece\u5916\u90e8 api (https:\/\/dummyjson.com\/recipes\/1) \u83b7\u53d6\u914d\u65b9\uff0c\u5e76\u4f7f\u7528 twig \u5c06\u5176\u5448\u73b0\u4e3a markdown \u6587\u6863\u3002 \u8fd9\u79cd\u65b9\u6cd5\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408 symfony \u5f3a\u5927\u7684\u8f6f\u4ef6\u5305\uff08\u4f8b\u5982\u7528\u4e8e\u5916\u90e8\u6570\u636e\u68c0\u7d22\u7684 symfony http \u5ba2\u6237\u7aef\u548c\u7528\u4e8e\u6e32\u67d3\u89c6\u56fe\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48552","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48552"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48552\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}