通过发送变量在php中执行helc.pl文件(exec hello.pl file in php by sending variable)
我有一个文件hello.pl,我想在php中调用。 我使用了exec(perl perl_tests \ hello.pl)和我
想把变量发送到这个pl文件你能给我建议吗?
I have a file hello.pl and i want to call in php. I used exec(perl perl_tests\hello.pl) and I
want to send variables in to this pl file can you give me advice?
原文:https://stackoverflow.com/questions/21452104
最满意答案
你真的很接近,唯一的问题是变量的内部,即
#{this_stuff}
都是在jade的上下文中执行的(没有这有点棘手,因为你在这里处理两个完全不同的javascript环境: 服务器端和客户端 。
因此,您需要在您的jade中输出服务器端变量,将其转换为将在客户端执行的javascript代码。
在相关的说明中,您可以在脚本块中使用Jade变量语法,但不能执行其他操作(如循环)。
首先,让我们清理它,以便所有
script
标记都在js
块中(假设您使用的示例KeystoneJS模板将位于<body>
标记的底部)并正确生成这些配置文件:block js script(src='https://maps.googleapis.com/maps/api/js?key=<GOOGLE_API_KEY>&sensor=false') script. var map; function initialize() { var mapOptions = { center: new google.maps.LatLng(51.0360272, 3.7359072), zoom: 8 }; map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); } google.maps.event.addDomListener(window, 'load', initialise); if data.profiles each profile in data.profiles script. new google.maps.Marker({ position: new google.maps.LatLng(#{profile.address.geo[1]}, #{profile.address.geo[0]}), map: map, title: "#{profile.name.full}" }); block content .container div(id="map-canvas", style="width:100%; height:700px;")
这种情况越来越近了(Jade会产生你现在所期望的),但是它还不会工作(因为你可能在
initialize
函数运行之前将标记添加到地图中)。它也没有逃避值,所以像名称中的
"
字符"
会导致语法错误。一种更健壮的方法是填充客户端数组,然后在创建映射后循环遍历该数组。 我们还将使用
JSON.stringify
来确保正确转义值。block js script(src='https://maps.googleapis.com/maps/api/js?key=<GOOGLE_API_KEY>&sensor=false') script. var map, profiles = []; function initialize() { var mapOptions = { center: new google.maps.LatLng(51.0360272, 3.7359072), zoom: 8 }; map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); for (var i = 0; i < profiles.length; i++) { new google.maps.Marker({ position: new google.maps.LatLng(profiles[i].geo[1], profiles[i].geo[0]), map: map, title: profiles[i].name }); } } google.maps.event.addDomListener(window, 'load', initialise); if data.profiles each profile in data.profiles script. profiles.push({ geo: !{JSON.stringify(profile.address.geo)}, name: !{JSON.stringify(profile.name.full)} }); block content .container div(id="map-canvas", style="width:100%; height:700px;")
请注意对!{variable}的更改,以便不转义JSON
最后,我建议在路径
.js
文件中为视图构建profiles
数组,而不是在jade模板中进行。 它更清洁,你不会在你的页面中找到大量的<script>
标签。所以你的路线看起来像这样(我假设有一点点给你的想法,并使用下划线使代码比vanilla javascript更整洁)
var keystone = require('keystone'), _ = require('underscore'); exports = module.exports = function(req, res) { var view = new keystone.View(req, res), locals = res.locals; // Load the profiles view.query('profiles', keystone.list('Profile').model.find()); // Create the array of profile markers view.on('render', function(next) { locals.profileMarkers = locals.profiles ? _.map(locals.profiles, function(profile) { return { geo: profile.address.geo, name: profile.name.full }; }) : []; next(); }); // Render the view view.render('profiles'); }
然后在您的视图模板中:
block js script(src='https://maps.googleapis.com/maps/api/js?key=<GOOGLE_API_KEY>&sensor=false') script. var profileMarkers = !{JSON.stringify(profileMarkers)}, map; function initialize() { var mapOptions = { center: new google.maps.LatLng(51.0360272, 3.7359072), zoom: 8 }; map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); _.each(profileMarkers, function(profile) { new google.maps.Marker({ position: new google.maps.LatLng(profile.geo[1], profile.geo[0]), map: map, title: profile.name }); }); } google.maps.event.addDomListener(window, 'load', initialise); block content .container div(id="map-canvas", style="width:100%; height:700px;")
You're really close, the only problem is that the inside of the variable, i.e.
#{this_stuff}
is all executed in jade's context (which won't have theIt's a bit tricky because you're dealing with two completely different javascript environments here: server side and client side.
So you need to output server-side variables in your jade, into javascript code that will be executed client-side.
On a related note, you can use Jade variable syntax in script blocks, but can't do other things (like loops).
Firstly, let's clean it up so all your
script
tags are in thejs
block (which, assuming you're using the example KeystoneJS templates would be down the bottom of the<body>
tag) and get those profiles being generated correctly:block js script(src='https://maps.googleapis.com/maps/api/js?key=<GOOGLE_API_KEY>&sensor=false') script. var map; function initialize() { var mapOptions = { center: new google.maps.LatLng(51.0360272, 3.7359072), zoom: 8 }; map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); } google.maps.event.addDomListener(window, 'load', initialise); if data.profiles each profile in data.profiles script. new google.maps.Marker({ position: new google.maps.LatLng(#{profile.address.geo[1]}, #{profile.address.geo[0]}), map: map, title: "#{profile.name.full}" }); block content .container div(id="map-canvas", style="width:100%; height:700px;")
This is getting closer (and the Jade will generate what you're expecting now), but it won't work (yet) because you're potentially adding the markers to the map before the
initialize
function has run.It's also not escaping the values, so things like a
"
character in the name would cause a syntax error.A more robust way of doing it would be to populate a client-side array then loop over that after the map has been created. We'll also use
JSON.stringify
to make sure the values are escaped properly.block js script(src='https://maps.googleapis.com/maps/api/js?key=<GOOGLE_API_KEY>&sensor=false') script. var map, profiles = []; function initialize() { var mapOptions = { center: new google.maps.LatLng(51.0360272, 3.7359072), zoom: 8 }; map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); for (var i = 0; i < profiles.length; i++) { new google.maps.Marker({ position: new google.maps.LatLng(profiles[i].geo[1], profiles[i].geo[0]), map: map, title: profiles[i].name }); } } google.maps.event.addDomListener(window, 'load', initialise); if data.profiles each profile in data.profiles script. profiles.push({ geo: !{JSON.stringify(profile.address.geo)}, name: !{JSON.stringify(profile.name.full)} }); block content .container div(id="map-canvas", style="width:100%; height:700px;")
Note the change to !{variable} so the JSON isn't escaped
Finally, I'd recommend building up the
profiles
array in your route.js
file for the view, rather than doing it in the jade template. It's a lot cleaner, and you won't end up with heaps of<script>
tags in your page.So your route would look something like this (I am assuming a fair bit to give you the idea, and using underscore to make the code neater than vanilla javascript)
var keystone = require('keystone'), _ = require('underscore'); exports = module.exports = function(req, res) { var view = new keystone.View(req, res), locals = res.locals; // Load the profiles view.query('profiles', keystone.list('Profile').model.find()); // Create the array of profile markers view.on('render', function(next) { locals.profileMarkers = locals.profiles ? _.map(locals.profiles, function(profile) { return { geo: profile.address.geo, name: profile.name.full }; }) : []; next(); }); // Render the view view.render('profiles'); }
Then in your view template:
block js script(src='https://maps.googleapis.com/maps/api/js?key=<GOOGLE_API_KEY>&sensor=false') script. var profileMarkers = !{JSON.stringify(profileMarkers)}, map; function initialize() { var mapOptions = { center: new google.maps.LatLng(51.0360272, 3.7359072), zoom: 8 }; map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); _.each(profileMarkers, function(profile) { new google.maps.Marker({ position: new google.maps.LatLng(profile.geo[1], profile.geo[0]), map: map, title: profile.name }); }); } google.maps.event.addDomListener(window, 'load', initialise); block content .container div(id="map-canvas", style="width:100%; height:700px;")
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
没有必要仅使用两个空格来制表。 您可以使用任意数量的空格(一致)或制表符缩进。 唯一的怪癖是你不能同时使用空格和制表符,或者编译时会出错。 看到这个链接玉 。 It's not necessary to use only two spaces for tabbing. You can use any number of spaces (consistently) or a tab character to indent. The only quirk is you can't use both spaces ...
-
gulp-changed文档说第一个参数是 目标目录。 与你输入gulp.dest()相同。 尝试更换线路 .pipe(changed('build/prototypes/', {extension: '.html'})) 同 .pipe(changed('build', {extension: '.html'})) The gulp-changed documentation says that the first parameter is The destination directory. Sam ...
-
使用jQuery和Jade(using jQuery with Jade)[2023-06-22]
在你的Jade文件中,你可以把: script. var book = '#{book}' script(src='someFile.js') 现在,在someFile.js中 ,您可以像使用普通变量一样使用书籍: $(document).ready( function() { console.log(book.title); }); In your Jade file, you can put: script. var book = '#{book}' script(src=' ... -
在Jade中调用每个函数(Calling a function on each in Jade)[2023-12-05]
你真的很接近,唯一的问题是变量的内部,即#{this_stuff} 都是在jade的上下文中执行的(没有google对象,因为这是客户端)。 这有点棘手,因为你在这里处理两个完全不同的javascript环境: 服务器端和客户端 。 因此,您需要在您的jade中输出服务器端变量,将其转换为将在客户端执行的javascript代码。 在相关的说明中,您可以在脚本块中使用Jade变量语法,但不能执行其他操作(如循环)。 首先,让我们清理它,以便所有script标记都在js块中(假设您使用的示例KeystoneJ ... -
如何渲染jade模板并使用jquery在div中调用?(How to render jade template and call that in a div using jquery?)[2022-03-24]
res.render不会返回任何内容,但会进行回调。 所以,请改为: res.render('somepage', { title: 'someotherpage' }, function(err, some){ if (err) return next(err); res.send(some); }); mentioning the route in index.js and restarting the application fixed the issue for me. -
你现在可能已经想到了这一点; 但是如果有其他人想知道同样的事情(就像我做的那样):通过将'templates'任务设置为'jade-watch'的依赖关系,你确保它在触发重载之前已经完成。 You might have figured this out by now; but in case anyone else comes along wondering the same thing (as I did): by setting the 'templates' task as a dependency ...
-
怎么链包括在玉?(How to chain includes in Jade?)[2022-11-18]
不要忘记你也可以直接在Jade文件中包含,或者只是在jade格式化的包含字符串上使用jade.render (例如jade.render的下面内容)。 看起来似乎没有一个很好的理由像在示例中那样进行递归包含。 只需根据需要在Jade文件中添加include。 就像是: exports.overview = function(req, res, next) { var jade = require('jade'); res.render('A', { main: jade.renderFi ... -
创造一个功能玉(create a function jade)[2022-08-05]
它基本上归结为向locals对象添加一个函数。 你可以在几个不同的地方做到这一点。 路线: app.get('/test', function (req, res) { res.locals.someFunction = function () { /* do stuff */ }; }); 一般中间件: app.use(function (req, res, next) { res.locals.someFunction = function () { /* do stuff */ }; n ...