Comments (9)
So, vash itself will run in any environment that provides ES5 compatibility. But what you're running into is that the layout helpers need a way to load a file. And to do that they assume that either:
- the
fs
andpath
modules are available to read files from the local filesystem and then cache it invash.helpers.tplcache
vash.helpers.tplcache[TEMPLATEPATH]
contains a compiled template (this is how it works in a browser)
So if you want to use another environment, you'll need to either:
- prepopulate
vash.helpers.tplcache
with every template you'll ever need (I recommend this) - replace
vash.loadFile
with a method that can load files in your environment
from vash.
Thanks, Andrew.
Since I actually do provide replacements for fs
and path
(at least I think so) and still see the error "Cannot call method 'normalize' of undefined" on a path.normalize
call, I'm currently trying to find the reason.
Can it be that vash creates kind of "context" when processing a template that makes my global replacements for fs
and path
somehow disappear? Can this be related to your solution for #37 where you show me how to make the require
available to templates?
from vash.
How are you providing the replacements? Vash calls var path = require(βpathβ)
, so if youβre just providing it as a global itβs going to
be overridden if the require
call returns nothing.
Yip yip,
~ Drew*
On Tue, Apr 29, 2014 at 12:30 AM, Uwe Keim [email protected] wrote:
Thanks, Andrew.
Since I actually do provide replacements for fs and path (at least I
think so) and still see the error "Cannot call method 'normalize' of
undefined" on a path.normalize call, I'm currently trying to find the
reason.Can it be that vash creates kind of "context" when processing a template
that makes my global replacements for fs and path somehow disappear? Can
this be related to your solution for #37https://github.com/kirbysayshi/vash/issues/37where you show me how to make the
require available to templates?β
Reply to this email directly or view it on GitHubhttps://github.com//issues/39#issuecomment-41640753
.
from vash.
That did it! π
I'm now putting the following snippet at the very first line before loading the "vash.js" content:
function require(name) {
return host.require(name);
}
With host
being my C#-provided mockup object that I add to the engine via:
_hf = new HostFunctions();
_engine.AddHostObject(@"host", HostItemFlags.GlobalMembers, _hf);
And in turn implementing HostFunctions
like this:
public class HostFunctions
{
public object require(string name)
{
switch (name)
{
case "fs":
return new MockFs();
case "path":
return new MockPath();
default:
throw new Exception();
}
}
}
I'm not yet through this completely, hope it's OK to ask a further question below.
from vash.
Last thing (hopefully):
I had to modify your sources. At 2 occurrances, I had to replace:
window['vash'] = vash
with
vashHolder = vash
That I defined before loading the "vash.js" content as:
var vashHolder = {};
Then I could call Vash with:
...
var tpl = vashHolder.compile(content);
...
The reason I had to do this was that the environment checks for AMD, NODSJS and BROWSER used the BROWSER case as a fallback which does not apply to my case.
My question
Is there a way to use Vash somehow like above without modifying your sources?
from vash.
Really interesting runtime, I'd not seen ClearScript before.
I'll add my 2c to this, the hoops you are jumping through at the moment are that ClearScript allows you to execute JavaScript and bind between .NET, but ClearScript doesn't support CommonJS (which is probably a good choice to make at a low level).
Vash determines the exporting type by trying to feature detect functionality of each distinct module syntax, for Node.js style (CommonJS) vash checks to see if 'module' is an object and that is has an exports property (see https://github.com/kirbysayshi/vash/blob/master/build/vash.js#L17).
You have a few options, the easiest is probably to define 'window' as a global, vash will then fallback and export itself to window.
Alternatively you can aim to make ClearScript support CommonJS, this may have already been done before, but if not you'll want to define a global called 'module' that has an exports property that is an object.
After vash is has executed, the module.exports property will be populated.
Another thing that may help from the vash source side is not to assume the global object is 'window', but to export to 'this' from a function being invoked at the global level: http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function
from vash.
Thanks, @meandmycode I first tried to mock a window
object but most likely I did something wrong on this, as I did not manage to make it working fully.
In addition the require
calls are only executed when window
is not defined, so this does not seem to be a solution for my case.
from vash.
So my final question remains:
Is there a way to use Vash inside ClearScript (as described above) without modifying the sources of Vash?
from vash.
@UweKeim, @meandmycode is right, the easiest thing for you to do is to just define a window
as a global within your execution environment. I had never heard of ClearScript before today, but this was recommended via another thread:
_engine.Execute("window = this");
That should at least let vash export itself properly.
from vash.
Related Issues (20)
- Feature Request: Add support for render method HOT 5
- How to make multiple includes for the same component work? HOT 2
- <a> is not considered a void tag, should it?
- ..
- vash block error with koa HOT 6
- Vash HOT 2
- @foreach razor syntax HOT 3
- Conditions inside templates HOT 1
- <a href="@item.html">... HOT 3
- Missing output when using if/else if/else followed by another if HOT 3
- Use syntax highlighting in README.md HOT 1
- Output for blocks with the commandline tool or compile API HOT 2
- [question] How to use vash.helpers.include HOT 1
- Is this library no longer developing? HOT 2
- UnclosedNodeError: Found unclosed VashMarkup HOT 2
- Add Support for Tag Helpers
- Add support for compile file
- Trying to get in touch regarding a security issue HOT 2
- Add TypeScript definitions
- Error running the CodePen playground
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vash.