NetTalk Central

Author Topic: How to insert external HTML snippet after fields on NetWebForm?  (Read 2605 times)

RWeHavingFunYet?

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
How to insert external HTML snippet after fields on NetWebForm?
« on: January 05, 2012, 12:22:24 PM »
When using a NetWebForm, I need to be able to insert an HTML snippet from an external file so that it appears below entry fields.

Note that I am using the Tab Wizard effect, so the HTML snippet will be different for each tab of the wizard.


I know that to insert the external file normally in a header or footer I would use this:

<!-- Net:f:FileName -->

But how do I insert it in this situation?


There is also one other related case (without trying to be too complex for this post).

In some instances, I am actually loading different snippets based on a session variable, so in reality I need to be able to insert a "token" ( like 'MyHTMLSnipTab1' ) after the fields instead of  <!-- Net:f:FileName --> so that I can "catch" that in the _SendFile embed point.  Then based on the token value I change the value of p_FileName so that it loads the correct snippet.

So how do I solve this?

ADDENDUM:

Actually in checking how I handle that in the header or footer, I see that I insert the "token" as the filename in the <!-- Net:f:FileName --> code.

So per the example above:  <!-- Net:f:MyHTMLSnipTab1 -->

Then I catch the token and replace it with the full path and filename to the externak snippet.

That said - all I need to know is how to insert the <!-- Net:f:MyHTMLSnipTab1 -->  after the other fields of the tab wizard.

Thanks!
« Last Edit: January 05, 2012, 02:53:06 PM by RWeHavingFunYet? »

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: How to insert external HTML snippet after fields on NetWebForm?
« Reply #1 on: January 05, 2012, 09:58:26 PM »
Hi Charles,

ok, so this breaks down into two parts;

a) how to inject a file into a form, as a form-field. and
b) how to make this filename a variable.

the first part is easy enough -
a) Create a form field of type "display"
b) Set the "text field" to say '<!-- Net:f:english.htm -->'
and
c) (importantly) tick on Allow xHtml.
(see attached pic a1.png).

The second question is more interesting because you have a number of possible options here. You could
a) translate the filename before it is used
b) set some tag, which is then translated by the Translate method or
c) translate the filename as it is interpreted (ie when the file is loaded)

I'll detail all 3, you can decide which one to use.

1.  Translate the name in the form, before the tag is added to the form.

If you look at the generated code, and search for english.htm, you'll find the following block;

 If Not (1=0)
  ! --- DISPLAY --- or ---- BUTTON
  loc:javascript = 'data-do="onclick" id="incfile"'
    packet = clip(packet) & |
      p_web.Translate('<!-- Net:f:english.htm -->',1) & |
      '<13,10>'
  do SendPacket

It's not difficult to make the '<!-- Net:f:english.htm -->' an expression.

a) Create a local variable, loc:snippet.
b) Replace '<!-- Net:f:english.htm -->' with loc:snippet in the form field template settings.
c) In the embed, right before it is used, set loc:snippet to either say
'<!-- Net:f:english.htm -->' or '<!-- Net:f:spanish.htm -->' or whatever.

Aside: The local variable is safe in this case because it is set and then used immediately, in the same block of code.

2. Translate the name using the Translate method.

As you can see the call in the above code is
p_web.Translate('<!-- Net:f:english.htm -->',1)
If you replaced '<!-- Net:f:english.htm -->' with say 'xSnippet1'
Then your translation mechanism could translate xSnippet1 into
'<!-- Net:f:english.htm -->' or (say) '<!-- Net:f:spanish.htm -->'

If I was going to do this a lot I might be tempted to make a sort of standard. For example calling the file something.eng.htm then the translate method only needs to look for .eng. and replace with .esp. say, without knowing the rest of the filename (or indeed caring about it).

If you were going to do a lot of these snippets then this "generic replacement" seems like the easiest way to go, while making the form code easy to understand (because when editing the form you can at least immediately see the default file which will be used there.)

3. Translate the tag when the file is loaded.

Both of the above techniques expose one problem - if the alternate language version of the file does not exist then the user sees nothing. It does not "fall back" on the original english version.

A somewhat more sophisticated approach takes the idea from number 2 (generic names) and do the translation at the point when the file is loaded. Imagine that we have 2 files welcome.eng.htm and welcome.esp.htm. In the program code, for the form display field, you simply include '<!-- Net:f:welcome.eng.htm -->'

If you follow the code in netweb.clw, you'll see that the loading of the file is done by the WebHandler._SendFile method. This method takes, as its first parameter, the name of the file to send. So you could embed something like the following in the WebHandler procedure, _SendFile method, before the parent call. This sample code uses StringTheory to simplify the string handling, but you can use normal Clarion string handling if you prefer;

str   stringtheory
  code
  str.setvalue(p_FileName)
  if str.instring('.eng.')
    str.replace('.eng.',self.GSV('languageprefix'))
    if exists(str.GetValue())
       PARENT._SendFile(str.GetValue(),p_header)
       return
    end
  end


Obviously the session value of languageprefix contains the language code for the current user.

The main advantage of this approach is that it's completely generic (it'll work on any number of files) and if the translation is missing it falls back on the originally included file. Plus the form contains the actual file name (of the english version anyway) which makes the form easier to maintain.

cheers
Bruce





[attachment deleted by admin]
« Last Edit: January 05, 2012, 10:24:33 PM by Bruce »

RWeHavingFunYet?

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: How to insert external HTML snippet after fields on NetWebForm?
« Reply #2 on: January 06, 2012, 01:47:34 AM »
Thanks