简体   繁体   English

如何将参数从PHP文件传递到模板?

[英]How to pass parameters from a PHP file to the template?

I have a website made with WP (a customer passed me). 我有一个使用WP创建的网站(客户通过了我)。 He want to see in one of the pages, a list of product categories, and to get the list of this product categories, I have to send a request to an API in another website. 他想在其中一个页面中看到产品类别列表,并想要获得该产品类别的列表,我必须向另一个网站中的API发送请求。 The site will response with an XML that contain the categories. 该站点将使用包含类别的XML进行响应。 To make the request I will use some PHP library. 为了发出请求,我将使用一些PHP库。 After the response is arrived, I want to show those categories in the page of my site of my customer. 收到响应后,我想在客户网站的页面上显示这些类别。

I have followed the first answer here to call a php file before rendering a template, but imagine that I want to pass a variable (product categories) from the php to to the template. 我也跟着第一个答案在这里调用PHP文件呈现模板之前,但是想象一下,我想从PHP变量(产品类别)传递给到模板。 How can I do that? 我怎样才能做到这一点?

You will probably want to cache the results of the XML that comes back to you (unless it is truely dynamic), and possibly store it in a table. 您可能想要缓存返回给您的XML的结果(除非它是真正的动态),并可能将其存储在表中。 You can decide how often you need to refresh the cache. 您可以决定刷新缓存的频率。 (This will protect your site and their API from DOS attacks, or even just high volume). (这将保护您的站点及其API免受DOS攻击,甚至只是防止大量攻击)。

Once you have your data stored in a table, your template can simply retrieve it. 一旦将数据存储在表中,您的模板就可以轻松检索它。

This pattern will reduce the coupling between the two parts of your solution, and make things a bit easier to build / debug. 这种模式将减少解决方案的两个部分之间的耦合,并使事情更容易构建/调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM