Create dynamic code with templates based on options - c#

I've a question related to dynamic code generation (html) with C# based on a template. The user sets all options (e.g. whether or not a <div> should be displayed, whether or not an image in the template should change by a specific date,...) and provides the content (text, images) needed for code generation. The generated code is needed for a WIFI hotspot site.
The template consists of HTML, CSS and JavaScript which gets extended and modified by the user-defined settings at runtime. I also think of providing "meta settings" in the template to define, whether or not some options CAN be disabled set or not.
My major problem:
How to define the template to dynamically extend the template code easily? For example, if a user option is enabled at runtime, I'm in need to add JavaScript code on top and HTML code below another (specific) place. Another example is to hide content (a defined ) when another option is disabled...
I'm not sure what's the best practice to handle that requirement. Maybe HTML elements with id attribute can help eliminate some of the problems. But with JavaScript I'm not sure. Maybe the template needs to be a XML which creates the final HTML at runtime? Any idea?
Has anyone an idea how to handle that?
UPDATE/INFO:
The project is written in pure C# - without any ASP.NET. It is a desktop project working with HTML files and GENERATING HTML files (as an output).

I suppose you can use ASP.NET MVC Razor view engine to render an html to a file with the help of this question. With Razor you will get the support of dynamic view (aka template) changes, rich template syntax etc. Everything you can do when create a web site. Just render the html to a file not a response body.

Related

creating many instances of a webpage from one template?

I'm creating a new website application in asp.net. The landing page needs to have a button (or something similar) which the user can click to create a new instance of a webpage. Similar to how a Facebook user can create a new group/event or a StackOverflow user create a new question.
My website needs to be able to create multiple "events" from the landing page which can then be accessed from the landing page, each event should be a template populated with user details on creation.
Can someone please tell me how people refer to this technique of creating many instances of a webpage (event) from one template?
With ASP.NET Core MVC (using this as an example as you have an ASP.NET tag and your description doesn't specify a technology), you can create a template using a .cshtml file. If you are not familiar with these types of files (which are used within the ASP.NET framework), then I suggest a read of it here:
https://www.w3schools.com/asp/razor_syntax.asp
Roughly, it's a file with HTML content where you can easily embed .NET types (such as types from your Model) and .NET logic using "Razor syntax", so that your HTML file is modified appropriately (e.g. with queried data specific to your user) before being sent back to the client. The reference above gives good examples, so I'm not going to waste space and repeat them here.
You can have certain .cshtml files as your "template" and embed appropriate model data using Razor syntax. You can then have a hyperlink tag (for example) reference the .cshtml file using the asp-action attribute. This will render the .cshtml file to the client whenever that tag is clicked on. ASP.NET uses types called Controllers to handle such requests (Controllers are types that inherit from the Controller type) appropriately, such as querying the correct database and providing your .cshtml file with the correct data before sending the result back to the client.
ASP.NET Core MVC modularizes the types of actions described above very well (M --> Model, V --> View, C --> Controller). Here is a good reference:
https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2
For other technologies that you wish to use to achieve the same result, you will have to consult the appropriate references.
Stackoverflow is a place to get answer on specific problem with short answer. Your problem is general design and programming question, and requires understanding of basic programming aproaches.
For that you should grab a book and read about designing webapplications in .Net.

Yet another .net custom cms

I'm trying to switch my custom cms written in php into .net c#. I was thinking to use cms as a learning project. I'm going to use C#, NHibernate ORM layer, mssql, mvc3 and jquery.
I'm aware there are plenty of commercials or open source cms, but still I'm going to spend some of my free time trying to learn new technology working on project like this.
So, is there anyone out there who is willing to share some ideas on creating cms domain model, usefull link, ideas, etc.
Thanks
A really basic CMS consist of 3 elements:
one database table to hold your "pages". The table structure is "name" and "content"
a route to transform requests of type /cms/pagename to a fixed controller, the method called cms and pagename as a parameter
a embeddable html editor
Now, there are two ways your "page" can be invoked. It is either create mode or view/edit mode.
In "create" mode, the page is requested but it is not in the database yet (e.g. cms/announcement1). In this mode you create a view consisting of a html editor and upon submit, you persist the page to the database.
In "view" mode, the page is requested and is IS in the database. You perform any necessary rewriting (for example you rewrite internal links of the form [cms/pagename] to a fully routable http address) and render the content.
If the user is authorized to edit the page, you also show a "edit" button which then invokes the html editor with the page loaded and ready to be edited.
And that's it.
There are tons of additional elements (caching, different built-in page types, embedding images, youtubes, preformatted texts) etc. but all of them are optional and you can introduce new features when you have the core already implemented.
Once I wrote a simple CMS following the structure above, it was a part of a bigger solution and till now it's been sucesfully deployed several hundred times. An advantage of a custom CMS is that it can be really simple and easily maintanable.

Use Wordpress Template in c# project

There are plenty of Wordpress template out there, most of them are well designed.
I like to know, is there a way to easy to use Wordpress template in c# project.
Is there any out-of-the-box solution?
Just grab a copy of the output HTML and the CSS of the WP site and apply/build your ASP.NET site around it. This is straightforward in itself. Where most people get confused is when there's a master page in the mix in ASPNET; however, this is not a problem. Just start (with the HTML produced by the WP site) by creating the main divs working from the outside-in, using the master page and then content areas in your pages. Master page(s) will generally contain display elements common to all or a subset of pages.
No, there is nothing "out-of-the-box".
You will need to write your own converter if you wish to do this, though translating a single page shouldn't be too difficult.

Localizing JavaScript strings in an ASP.NET Web Forms application

One of the apps I work on is a large ASP.NET 3.5 Web Forms app that is used in the U.S., Central and South Americas, and Europe. We're also starting to do more work with AJAX, and thus we need to settle on a strategy for localizing strings in our JavaScript controls.
For example, we have a control written as a jQuery plugin that formats data into a sortable table. The buttons and table columns need to be localizable.
We're currently using two different approaches to handle this scenario, but I'm not completely satisfied with either.
Write the bulk of the code in a jQuery plugin style, then place a script block on the .aspx page where we'll pull in values from a .resx file and feed them into the plugin code. Here's an example in pseudo code:
<script>
var view;
$(function() {
view = {
columnHeaders: {
productNumber = <%$ Resources:WidgetProductNumber_HeaderText %>,
productDescription = <%$ Resources:WidgetProductDescription_HeaderText %>
}
};
});
</script>
Place the JavaScript in plain .js files with custom tokens in place of strings. We have a handrolled HttpModule that will parse JavaScript files and replace the tokens with values from any existing .resx file whose file name matches the name of the JavaScript file being processed.
Both approaches have problems. I'd prefer to keep our JavaScript code separate from our .aspx pages to make it more unobtrusive and reusable.
The HttpModule approach is clever but a little opaque to developers. I'm also looking to implement a JavaScript bundler called Rejuicer, which is also written as an HttpModule, and getting these to work together seems like it would require customizing the open source code. I'd prefer to use the code as it's written so that we can upgrade it as the project progresses.
Are there any other tried-and-true strategies for approaching this problem in ASP.NET?
It seems that both approaches are a little more complex/cumbersome than necessary. Keep it simple.
1) Using an .ashx, custom http handler, or web service, create a .net object (anonymous, custom -- doesn't matter) that matches the client side JSON object.
2) Populate server side object's properties with the localized values
3) Set the response content type to text/json or text/javascript.
4) Using the JavaScriptSerializer class, serialize the object into the response stream.
From the client side, you have two options:
1) Use an AJAX call to the .ashx/handler/service to set your client side "view" object to the response JSON.
2) Create a script tag with the src="the/path/to/the/serviceOrHandler". In this case you would need to include the js variable declaration in your response output.
Let me know if you need a code sample.
I just stumbled onto this question, and I have another answer to throw into the ring. It isn't my work or anything, but it looks like a fairly elegant solution. It involves writing a localization handler to serve up ASP.NET resources to Javascript. Here are a couple of links:
http://weblog.west-wind.com/posts/2009/Apr/02/A-Localization-Handler-to-serve-ASPNET-Resources-to-JavaScript
http://www.tikalk.com/use-aspnet-resource-strings-within-javascript-files/

Preset html files built into program?

I'm trying to create a very basic web site creator in C#. I already created the templates but have no idea how to get them in the program so the user can pick one and customize it. Is there a certain library I should look into? I've bee told this can be easily created in php but I'd rather not go down that route.
If using .NET you can use WebControl controls to display a preview of your templates and make the user decide which one he wants.
Or a list of templates and single WebControl that parse and display the HTML of the current selected list item (template) and then make the user select.
To edit, just a bunch of controls (one for each template placeholder) that updates the view of the WebControl.
In the top link you found how to do it.
P.S. Templates can be stored on XML, Plain Text, DB or any other mean.
Either store the templates as files on the web-side and use them as templates, or store them in a DB. Unless your app is not web-based, in which case the same advice pretty much applies.

Categories