I need to render a block of html from a template into a string, then in the controller apply some logic (for example escaping the html tags) and then include it in another view.

asked 14 Nov '11, 09:31

pedrolima's gravatar image

pedrolima ♦♦
1.1k232840
accept rate: 32%

edited 02 Feb '12, 11:10


To achieve this:

  1. reinitialize the view with a custom writer object
  2. process the view
  3. retrieve the output from the writer object

Let's suppose we have a view, block.htm. that we want to render to a string, encode as html, and include the result in another view main.htm.

Views:

-- block.htm
<%@page language="abap"%>
<strong>The name is <%= name %></strong>

-- main.htm
<%@page language="abap"%>
<html>
<body><%= blockader %></body>
</html>

Our controller code would look like this:

method DO_REQUEST.
  DATA: view TYPE REF TO if_bsp_page,
      main_view TYPE REF TO if_bsp_page,
      writer type ref to cl_bsp_writer,
      prev_writer type ref to if_bsp_writer,
      render_block type ref to cl_bsp_page_base,
      block_content type string,
      elem type ref to if_bsp_element.

* Create the view and initialize the attributes.
  view = create_view( view_name = 'block.htm' ).
  view->set_attribute( name  = 'name' value = 'Rose' ).

* Render the view and retrieve the resulting html from the writer object
  create object writer
    EXPORTING
      previous_writer = prev_writer.

  render_block ?= view.
  render_block->do_reinit( current_element = elem
                          current_writer  = writer ).

  render_block->do_request( ).
  block_content = writer->get_content( ).

* block_content holds the rendered view as a string.
* do whatever processing you need
  block_content = cl_bsp_utility=>encode_string( encoding = if_bsp_writer=>co_html
                                                in = block_content ).

  main_view = create_view( view_name = 'main.htm' ).

* you can pass the rendered view as an attribute to include it in another view
  main_view->set_attribute( name  = 'blockvar' value = block_content ).

  call_view( main_view ).
endmethod.
permanent link

answered 13 Dec '11, 11:20

nuno's gravatar image

nuno ♦♦
162229
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×28
×6
×4

question asked: 14 Nov '11, 09:31

question was seen: 17,452 times

last updated: 02 Feb '12, 11:10