(对于大师)在PHP中的网页中嵌入函数? [英] (For gurus) Embed functions in webpages like in PHP?

查看:55
本文介绍了(对于大师)在PHP中的网页中嵌入函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是PHP程序员,为一个新项目评估Python。

我真的很喜欢Python的很多概念,特别是shell,但有一个很好的PHP功能,我不知道如何用Python代替:


对于数据库密集型网页,我喜欢在文件系统中快速缓存HTML输出

,并在以后再次请求相同的URL

时放出文件,例如:


/ cache / some_url:

< html>

生成20分钟的重要数据库结果

< / html>


和:

/www/master_script.php:

<?

if(is_file(" / cache / $ URL"))include(" / cache / $ URL");

else include(" /scripts/soo_long_and_complicated.php" ;);

?>


到目前为止一直这么好,在
$中做类似的事情不应该太多问题b $ b Py然而,有时需要仍然访问数据库,例如

来旋转横幅,显示用户指定的样式表,更改输出

取决于使用的浏览器等。

在PHP中,这样做很简单,而不是缓存函数的输出,

我只是缓存"<?功能(); ?>",所以示例如下:


< html>

<? echo random_banner(); ?>

生成20分钟的重要数据库结果

< / html>


我用过这项技术现在大约一年了,它适用于

半静态页面(手动更改)或一些不太快

更改动态页面,在某些情况下我将数据库负载减少到20%。

它的美妙之处在于我可以使用相同的random_banner _()功能为

缓存和动态。


据我所知,没有办法在网页中嵌入Python代码与

"<? ?>"或任何等价的东西,我没有看到任何其他方法来解决这个问题。


但也许其他人这样做,我会先发现所有想法,


非常感谢


-

我没有遭受精神错乱,我喜欢它的每一分钟

- Calvin

Hi,

I''m a PHP-programmer who evaluates Python for a new project.
I really like a lot of concepts of Python, especially the shell, but there
is one great feature of PHP which I don''t know how to replace in Python:

For database-intensive webpages, I like to cache the HTML-output on the fly
in the filesystem and put out the file later whenever the same URL is
requested again, for example:

/cache/some_url:
<html>
important database result that took 20 minutes to generate
</html>

and:
/www/master_script.php:
<?
if (is_file("/cache/$URL")) include("/cache/$URL");
else include("/scripts/soo_long_and_complicated.php");
?>

So far so good, shouldn''t be too much of problem to do something similar in
Python.

However, it''s sometimes necessary to still access the database, for example
to rotate banners, show user-specified style-sheets, change output
depending on the used browser, etc.
In PHP it''s trivial to do this, instead of caching the output of a function,
I just cache "<? function(); ?>", so the example looks like this:

<html>
<? echo random_banner(); ?>
important database result that took 20 minutes to generate
</html>

I''ve used this technique for about a year now and it works great for
semi-static pages (which are manually changed) or some not too fast
changing dynamic pages, in some cases I reduced the database-load to 20%.
The beauty of it is that I can use the same "random_banner_()" function for
the cache and on the fly.

As far as I''ve seen, there is no way to embed Python-code in a webpage with
"<? ?>" or anything equivalent and I don''t see any other way to solve this
problem.

But maybe somebody else does, I''d apreceate all ideas,

Thanks a lot

--
"I don''t suffer from insanity, I enjoy every minute of it"
- Calvin

推荐答案

URL"))include(" / cache /
URL")) include("/cache/


URL");

else include(" /scripts/soo_long_and_complicated.php");

?> ;


到目前为止一直这么好,不应该做太多类似的事情来完成类似的事情。

Python。


但是,有时仍需要访问数据库,例如

来旋转横幅,显示用户指定的样式表,更改输出

取决于使用的浏览器等。

在PHP中,这样做很简单,而不是缓存函数的输出,

我只是缓存"<?功能(); ?>",所以示例如下:


< html>

<? echo random_banner(); ?>

生成20分钟的重要数据库结果

< / html>


我用过这项技术现在大约一年了,它适用于

半静态页面(手动更改)或一些不太快

更改动态页面,在某些情况下我将数据库负载减少到20%。

它的美妙之处在于我可以使用相同的random_banner _()功能为

缓存和动态。


据我所知,没有办法在网页中嵌入Python代码与

"<? ?>"或任何等价的东西,我没有看到任何其他方法来解决这个问题。


但也许其他人这样做,我会先发现所有想法,


非常感谢


-

我没有遭受精神错乱,我喜欢它的每一分钟

- Calvin

URL");
else include("/scripts/soo_long_and_complicated.php");
?>

So far so good, shouldn''t be too much of problem to do something similar in
Python.

However, it''s sometimes necessary to still access the database, for example
to rotate banners, show user-specified style-sheets, change output
depending on the used browser, etc.
In PHP it''s trivial to do this, instead of caching the output of a function,
I just cache "<? function(); ?>", so the example looks like this:

<html>
<? echo random_banner(); ?>
important database result that took 20 minutes to generate
</html>

I''ve used this technique for about a year now and it works great for
semi-static pages (which are manually changed) or some not too fast
changing dynamic pages, in some cases I reduced the database-load to 20%.
The beauty of it is that I can use the same "random_banner_()" function for
the cache and on the fly.

As far as I''ve seen, there is no way to embed Python-code in a webpage with
"<? ?>" or anything equivalent and I don''t see any other way to solve this
problem.

But maybe somebody else does, I''d apreceate all ideas,

Thanks a lot

--
"I don''t suffer from insanity, I enjoy every minute of it"
- Calvin







查看 http://cheetahtemplate.org ,你会的有可能将

python代码放入模板中并拥有自动优雅的缓存......


此页面描述并显示非常清晰的示例
http://cheetahtemplate.org/docs/user。 ..g.regions.html


这只是一个例子,还有其他可能性,有很多网页

framewo rks。
http://www.python.org/ cgi-bin / moinmoin / WebProgramming

Robert Ferber< ro*@nospam.net>写道:

Hi,

Look at http://cheetahtemplate.org, you''ll have the possibility to put
python code into the template and have automatic and elegant cache...

This page describe it and show very clear examples
http://cheetahtemplate.org/docs/user...g.regions.html

It''s just an example, there are others possibility, with many web
frameworks.
http://www.python.org/cgi-bin/moinmoin/WebProgramming
Robert Ferber <ro*@nospam.net> writes:


我是PHP程序员,为一个新项目评估Python。
我真的很喜欢很多概念Python,特别是shell,但有一个很好的PHP功能,我不知道如何在Python中替换:

对于数据库密集型网页,我喜欢缓存文件系统中的HTML输出动态
,并在以后再次请求相同的URL时将文件放出,例如:

/ cache / some_url:
< html>
需要20分钟才能生成的重要数据库结果
< / html>

和:
/www/master_script.php:
< ;?
if(is_file(" / cache /
Hi,

I''m a PHP-programmer who evaluates Python for a new project.
I really like a lot of concepts of Python, especially the shell, but there
is one great feature of PHP which I don''t know how to replace in Python:

For database-intensive webpages, I like to cache the HTML-output on the fly
in the filesystem and put out the file later whenever the same URL is
requested again, for example:

/cache/some_url:
<html>
important database result that took 20 minutes to generate
</html>

and:
/www/master_script.php:
<?
if (is_file("/cache/


这篇关于(对于大师)在PHP中的网页中嵌入函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆