Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » HtmlTable class example request
HtmlTable class example request [message #46101] Mon, 07 March 2016 23:31 Go to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
Hello,

At moment my program produces Html tables but the code is a mess with zillion hardcoded tag. It is always a pain to move a column in another position when I need to update the program to met the user request.
So I surfed the net to find an easy way to make a Html table within c++ in an easy and clean way. No results. Instead in Java and other languages there a lot of libraries.
Then, as usual, looking in Core package I found what I need... if only I knew how to use it Rolling Eyes .
Unfortunately I have found no example.

Does anybody know how to use Web.h and class HtmlTable to set a simple table with header, two rows and two columns? Is it possible to format a tag (for example <tr>) with some string in order to use the CSS bind to the page to whom the table belongs?

Thanks a lot,
Luigi


Re: HtmlTable class example request [message #46102 is a reply to message #46101] Mon, 07 March 2016 23:48 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hi luigi,

I think you can use QTF to build you're table and then convert it to HTML
Should be quite easy to use
Re: HtmlTable class example request [message #46103 is a reply to message #46101] Tue, 08 March 2016 00:16 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
forlano wrote on Mon, 07 March 2016 23:31
Hello,

At moment my program produces Html tables but the code is a mess with zillion hardcoded tag. It is always a pain to move a column in another position when I need to update the program to met the user request.
So I surfed the net to find an easy way to make a Html table within c++ in an easy and clean way. No results. Instead in Java and other languages there a lot of libraries.
Then, as usual, looking in Core package I found what I need... if only I knew how to use it Rolling Eyes .
Unfortunately I have found no example.

Does anybody know how to use Web.h and class HtmlTable to set a simple table with header, two rows and two columns? Is it possible to format a tag (for example <tr>) with some string in order to use the CSS bind to the page to whom the table belongs?

Thanks a lot,
Luigi




Web.h is obsolete, please do not use with new projects. It will be removed from the next release.

As for "HtmlTable" and related classes, it was an attempt to reproduce success of SqlExp. It worked, however in the end it proved much less useful than SqlExp is.

Mirek
Re: HtmlTable class example request [message #46104 is a reply to message #46101] Tue, 08 March 2016 06:26 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

In my experience, only reasonable way to write html without way too much hardcoded stuff is to use templates. You could reuse templates from Skylark, it shouldn't be too complicated to use without the rest of the framework. You only might need to supply some configuration. The code would look something like this:
Renderer rr;
ValueArray rows;
// add your data to rows
rr("ROWS", rows);
String htmlTable = rr.Render("table.witz");

And the table.witz template would look similar to this, with endless styling options:
<table>
  <tr><td>Column1</td><td>Column2</td></tr>
  $for(i in RESULTS)
    <tr><td>$i.COL1</td><td>$i.COL2</td></tr>
  $/
</table>


Note: I haven't tested this, so there might be some mistakes Wink

Best regards,
Honza
Re: HtmlTable class example request [message #46107 is a reply to message #46104] Tue, 08 March 2016 12:06 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
dolik.rce wrote on Tue, 08 March 2016 06:26
In my experience, only reasonable way to write html without way too much hardcoded stuff is to use templates. You could reuse templates from Skylark, it shouldn't be too complicated to use without the rest of the framework. You only might need to supply some configuration.


Hi Honza,

it looks interesting.
Here is tutorial Skylark02 modified as you suggest:
#include <Skylark/Skylark.h>

using namespace Upp;

struct MyApp : SkylarkApp {
	MyApp() {
		ValueArray va;
		va.Add(1);
		va.Add("Hello");
		ValueMap m;
		m.Add("key1", "first value");
		m.Add("key2", "second value");
		
		Renderer rr;
		rr("MyValue", "some value");
		rr("MyRawValue", Raw("<b>raw <u>html</u></b>"));
		rr("MyRawValue2", "<b>another raw <u>html</u></b>");
		rr("MyArray", va);
		rr("MyMap", m);
		String htmlTable = rr.RenderString("Skylark02/index");
		SaveFile("out.html", htmlTable);
	}
};

CONSOLE_APP_MAIN
{

	MyApp().Run();	
}


It worked, i.e. the html file has been saved, BUT it run a server too that continue to listen. Is there a way to take advantage of Witz template, Renderer class but without running a server?
It would be great for HTML and maybe for QTF report too.

Instead this variant
#include <Skylark/Skylark.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
		ValueArray va;
		va.Add(1);
		va.Add("Hello");
		ValueMap m;
		m.Add("key1", "first value");
		m.Add("key2", "second value");
		
		Renderer rr;
		rr("MyValue", "some value");
		rr("MyRawValue", Raw("<b>raw <u>html</u></b>"));
		rr("MyRawValue2", "<b>another raw <u>html</u></b>");
		rr("MyArray", va);
		rr("MyMap", m);
		String htmlTable = rr.RenderString("Skylark02/index");
		SaveFile("out.html", htmlTable);	
}


just crashes.

edit: it seems RenderString() is not able to continue because I break out something important not inheriting from skylark Sad

Thanks,
Luigi

[Updated on: Tue, 08 March 2016 18:16]

Report message to a moderator

Re: HtmlTable class example request [message #46108 is a reply to message #46107] Tue, 08 March 2016 12:21 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

forlano wrote on Tue, 08 March 2016 12:06

struct MyApp : SkylarkApp {
    // ...
}


It worked, i.e. the html file has been saved, BUT it run a server too that continue to listen. Is there a way to take advantage of Witz template, Renderer class but without running a server?


The server is hidden in SkylarkApp, try not inheriting from it. Not sure how much things will break though Smile For start, I'd try to create a simple function from the code you have in the constructor and simply call it from main.

Honza
Re: HtmlTable class example request [message #46114 is a reply to message #46101] Tue, 08 March 2016 20:32 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi,

I just could not resist not to try myself Smile Here is the minimal code that works:
#include <Skylark/Skylark.h>

using namespace Upp;

void test() {
	ValueArray va;
	va.Add(1);
	va.Add("Hello");
	ValueMap m;
	m.Add("key1", "first value");
	m.Add("key2", "second value");
	
	Renderer r;
	r("MyValue", "some value")
	    ("MyRawValue", Raw("<b>raw <u>html</u></b>"))
	    ("MyRawValue2", "<b>another raw <u>html</u></b>")
	    ("MyArray", va)
	    ("MyMap", m);
	DUMP(r.RenderString("Skylark02/index"));
}

CONSOLE_APP_MAIN {
	StdLogSetup(LOG_FILE|LOG_COUT);
	SkylarkApp dummy;
	test();
}


There are two "interesting" things to notice: 1) Exactly one SkylarkApp instance must exist for the template engine to work. That is because there is a global variable used to access configuration. And 2) The code must be compiled with MT flag, because the Skylark package is (not surprisingly) written to be used with threads.

So the solution works, but it is kind of a hack Smile It might be actually useful, if the templating code could be refactored into separate package. Applications generating HTML (that are not servers) are quite common. What do you think Mirek, is it possible?

Honza
Re: HtmlTable class example request [message #46115 is a reply to message #46114] Tue, 08 March 2016 21:50 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
dolik.rce wrote on Tue, 08 March 2016 20:32
Hi,

I just could not resist not to try myself Smile


Well done!

your solution is very interesting. I hope this hack can become the first step toeard a templating engine.
I have two questions:

- Skylark need to be compiled with MT. I suppose I must compile even my program with this flag. Does it mean something in term;

- Shat that dummy SkylarkApp does? It can interact in some way or stay there as a dead code?

Thanks,
Luigi
Re: HtmlTable class example request [message #46116 is a reply to message #46115] Tue, 08 March 2016 22:13 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

forlano wrote on Tue, 08 March 2016 21:50
- Skylark need to be compiled with MT. I suppose I must compile even my program with this flag. Does it mean something in term;
Yes, I think it means the entire program must have MT flag. I tried to to trick it and it actually compiles as far as Core, Sql and Skylark packages have MT flag. The program even runs and works, but ends with segfault at the end of main. On the other hand, MT flag doesn't change much, as long as you don't use threads, so it is probably not a big deal...

forlano wrote on Tue, 08 March 2016 21:50
- Shat that dummy SkylarkApp does? It can interact in some way or stay there as a dead code?
Well, actually you can us this to your advantage. E.g. to set configuration, such as where the templates are stored or whether they should be cached. Example:
	SkylarkApp dummy;
	SkylarkApp::Config().use_caching = false;
	SkylarkApp::Config().path = "/some/path/templates";

But other than this, it is just a lot of dead code wrapping a configuration object.

Honza
Previous Topic: IntStr function returns an empty string
Next Topic: conversion from String to wchar_t
Goto Forum:
  


Current Time: Thu Mar 28 23:43:14 CET 2024

Total time taken to generate the page: 0.01123 seconds